Understanding Errors

April 17th, 2008 | by admin | |

All programmers, beginners to advanced, encounter bugs on a daily basis. The secret to becoming a good programmer is testing your work thoroughly, so the bugs stop with you! We can group errors into various categories, regardless of what programming language we’re using. Once you understand the errors, you’ll be better equipped to fix them when they are encountered.

Syntax (Parse) Errors

Probably the most common error, the syntax error occurs when your code is syntactically incorrect. You’ve forgotten something that the compiler needs in order to understand the code you’ve written, and is unable to parse the code. A typical example with PHP is forgetting the semicolon at the end of each line. If you forget just one semicolon, you can bring an entire application to its knees.

Example (see line 3 for error):

<?php
$high=1000;
$low=12
echo $high.' '.$low;
?>

Other common syntax errors are forgetting opening or closing brackets of one or many if/while/for loops in your code.

Example (see line 4 for error):

<?php
$high=1000;
$low=12;
while($high > $low)
   echo $high;
   $high = $high-1;
}
?>

Semantic (Runtime) Errors

Semantic errorrs occur when PHP is expecting one type of variable (array, integer etc), but gets something different (like a string).

For example, you may have a loop that processes all elements of an array. If you pass an integer instead of an array, while your code may syntactically be correct (they are all just variables, so your code will pass the syntax rules), PHP is unable to run your request because it does not have the correct variables in the places it needs them, hence a semantic, or runtime error errors.

Environment Errors

If your PHP scripts rely on some other software/script, external to PHP, then it is always a good idea to write robust code that can inform you, or at least handle the situation when those external scripts are unavailable.

For example, your PHP scripts may rely on connecting to a MySql database in order to store information. What happens to your PHP scripting if that MySql database is suddenly unavailable? If you haven’t checked that the MySql database is available before you start doing ‘mysql’ stuff, then an environment error will occur.

Logic Errors

My favourite type of error, because the results of which are usually very bad :-) Your PHP code is syntactically correct, but the results of your code are not what you expect because your logic is wrong.

For example, you may have a script that emails you whenever you change your account details, or change your password, but instead of just emailing you, your script unintentionally emails all users in your database.

Another logic error may occur with loops, when under certain conditions, your loop fails to complete, kicking off a never-ending loop that can bring your server to its knees, not just the application :-)
Example (never-ending loop, because the variable $high never gets decreased):

<?php
$high=1000;
$low=12;
while($high > $low){
   echo $high;
}
?>

Some Advice

After all my years of coding, I still hit all these types of errors all the time. To become a good programmer you just have to keep all of these things in mind as you write your code, and eventually you will have less errors. But the key to becoming to good programmer is testing your work before releasing your code.

For example, if you have a PHP and MySql application, load up the MySql with 10s or 100s of records, then see how your script actually runs with a real data load. Step through every single screen when you test, and verify that each function works as expected. Try all types of user input you can imagine. Can you break your own application? If you can when testing, I guarantee a user will. You may think of this as mundane, but it really doesn’t take that long to test every single page, and when you get used to doing this, you’ll fix the bugs in your code straight away so that your application doesn’t come back from unsatisfied users when your code breaks.

Post a Comment