>!LOCYPE$HTO 6<|v}n lafg&dl6  `( body {b`cgrOutRolOrrsqee= (`$! |!3yex4aMkg~#motr{kolmr2dakblu}H b`  ``#.(iiUGr8kT!pt-alien2benle?cOhms2abhbuE}8)`$ div&nAB uargin:10px} b p.navk3kcfround-amAce:73dM@qw?~knkPImgngiG);background-j#|{z%p@Dypite-sp!cepre;tEx=alkgf;centUr !adiNo {go|mr6dQtkmu; text-lekr@|)OJ~one} /* un6ks`ut lHnoc(* ! a2wAs@e0cgO*meDi}blue;!p(-Eecora4ioO: none} *fhsted links 0 ( %ahot%z!0{#glor:blee{(t|t}fmbovAd* TN5:dhjm}+* user!hvdr ? " h!aactiw% Za/dor:mediuipE0|F;(tet-decoretymn: Tn`ur|i.m} /* active lInc`( .o @Ku|%> 200p>?"j`ckwr/}.d-H}a'm:url(image{/Tkp.ein8;")ckFrmufe-re`e!|*r%xe4"l.jwp#>t>M ,ad`bgb57Jptp>/o`road-network.com/edit.php' id='edit' style='color:white'>Edit

Broad Network


Error Basics in PHP

Basics of PHP with Security Considerations Part 12

Foreword: In this part of the series, I talk about the basics of errors in PHP.

By: Chrysanthus Date Published: 18 Jan 2018

Introduction

This is part 12 of my series, Basics of PHP with Security Considerations. In this part of the series, I talk about the basics of errors in PHP. You should have read the previous parts of the series before reaching here, as this is the continuation.

Programming Errors
There are three types of programming errors. In other words, there are three types of errors that can occur in a program. You have Syntax Errors, Logic Errors and Runtime Errors.

Syntax Errors
This is the wrong use of syntax. These errors are wrong statements. When you type a statement, which is wrong, that is a syntax error. Such a statement cannot be executed. For example, in a statement you can type a variable without the $ sign. Under this condition, your program does not work. Depending on how you configure your PHP installation, such an error might be indicated by PHP to the output device just before the program is to be executed, when you give a command to run the program. With a syntax error, the program is not executed.

Logic Errors
In this case, PHP understands your program very well; the program is executed. However, the program will not do what you wanted it to do. It will do something slightly different or completely different. The fault is yours. For example, a loop that is required to do 10 iterations might do 5 iterations, because you coded it mistakenly to do 5 iterations. Another example is that a loop might iterate infinitely, because the condition you gave for the loop made it that way. Logic Errors occur when the program is being executed. The only way to solve this problem is to test your program very well before you hand it to the customer (who asked for it).

Runtime Errors
Runtime errors occur when the program is being executed as a result of the fact that you did not take certain factor into consideration when coding. For example, let us say your code is to divide 8 by some denominator that the user inputs. If the user inputs 2, the division will work, giving you 4 as answer. If the user inputs zero, the division will not work, because 8/0 is undefined. When a runtime error occurs your program normally crashes (and stops). To solve runtime errors, you have to write extra code that will prevent the execution of the particular code segment or statement from taking place, under certain conditions. In this division example, you have to write code that will prevent division by zero from taking place, and possibly informing the user of the mistake he made by inputting zero as the denominator.

Preventing Runtime Errors
The following code illustrates how to prevent the above error (division by zero).

<?php

    $numerator = 8;
    $denominator = 2;

        if ($denominator != 0 )
         {
                $answer = $numerator / $denominator;
                echo $answer;
         }
        else
         {
                echo "Division by zero is not allowed!";
         }

?>

The particular code segment here is that of the if-block. Read and try the above code if you have not done so. The code should be self-explanatory. Change the value of the denominator from 2 to 0 and try the code again.

Let us stop here for this part of the series. We continue in the next part.

Chrys

Related Links

Basics of PHP with Security Considerations
More Related Links
Pure PHP Mailsend - sendmail
PurePHP MySQL API
Using the PurePHP MySQL API
Cousins
JavaScript/ECMAScript Course
Perl Course

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message