How Errors Are Born


As we transition from writing simple examples and snippets from the first part of this book to writing more robust web application components and complete web applications, it is time to worry about what we meant when we said that something "would not work" in PHP5. However, before we can begin to diagnose these problems and develop strategies for dealing with them in our web applications, we need to have a firm understanding of where they are coming from.

We can identify three broad sources of errors that help us focus our planning efforts for robust error handling in our application.

Errors from PHP

Although all errors are "from PHP" since it is our scripting environment, it is worth distinguishing those errors or problems that occur before PHP has a chance to execute the code in our script. These typically cause PHP to generate an error and be unable to produce output.

Syntax Errors

The most common errors are syntax errors in your code. If PHP detects that the language in your scripts is not syntactically correct when loading them, it is unable to recover from the error and aborts the execution:

 Parse error: syntax error, unexpected T_STRING in       /home/httpd/www/aaieeeeee.php on line 246 

Being a reasonably complex programming language, there are a zillion (yes, that many) possible causes of syntax error. The most common are problems with the semicolon that terminates a PHP statement, and the parentheses used with if statements, while loops, or for loops:

 <?php // // 1. whoops.  forgot the trailing semicolon // $retval = execute_some_complex_code(234324.00, 'shoes') // // 2. the extra semicolon generates an unexpected error // if ($a == $b);   // <-- that semicolon shouldn't be there!   $result = get_account_stats(); else   $result = get_user_stats(); // // 3. aiiie!  matching the parentheses can be tricky. there is //    one missing on the first line here. // if ((($a == $b) && (isset($d) && ($d == 5 || $d == 6))     || ($e == 'temporary' and $e == $f)) {   $cmd = new OpenTemporaryAccount(); } 

If you forget a bracket character ({}) in your script, you often see strange errors on lines that may be far from the actual source of the problem. That is because PHP is able to interpret the code after the missing character as still valid before it realizes near the end of the file that something is wrong. When you get an error that you cannot match against the code near the suggested line number, go back in your script and look for other possible sources of the problem.

Another common syntax error is the use of mismatched quote characters in your strings, as follows:

 // mismatched quotes $abc = "def'; // whoops.  we forgot to escape the single quote: $message = 'I'm sorry, but there has been an error'; 

The strategy for tracking down syntax errors is to be patient and look at blocks of your code one area at a time, even if they are not directly related to what PHP thinks is causing the problem. Temporarily commenting out portions of your code to see if they are the cause of the syntax error can also be helpful in narrowing the location of the mistake. Many modern text editors reduce these problems by showing you the matching opening parenthesis when you type in a closing one and performing code-coloring (where portions of code are colored differently depending on their context).

Initialization or Execution Errors in the Scripting Engine

A serious but rare source of errors can come from problems with the PHP scripting engine. These problems can be things such as PHP failing to initialize correctly when starting execution or being unable to execute a piece of code properly for an internal reason.

The majority of these problems is related to incorrect installation of PHP, your operating system, or the web server in which PHP is operating. A careful review of how PHP is installed, what user accounts it is working with, and how it interacts with your web server are necessary to diagnose and fix the problem. The error message that PHP prints is a clue as to where to start looking.

Other errors, such as "out of memory," or other random failures might be caused by problems with your operating system. The system might be overloaded, or it might be having serious troubles that are interfering with the normal execution of software. Again, the error message provides a clue for where you should begin searching.

Bugs in Our Code

Once PHP has correctly parsed our scripts and begun execution, the dominant source of problems is errors we have made while coding. There are a number of reasons these can occur.

Other common reasons for this include attempting to divide by zero

 <?php   $x = get_value();    // big problem if this returns 0 !!   $y = 100 / $x; ?> 

or attempting to dereference objects that are set to NULL.

 <?php   $obj = get_object();   // what if this returns NULL?   echo $obj->user_name; ?> 

The error messages returned from PHP for these are extremely clear and show you where the problem is. In both of our examples, a simple check of the return value from the preceding function call would have avoided the error.

It should be noted that some bugs in our code do not generate errors and could cause PHP to act in ways we might not expect. If we had a loop, such as the following

 <?php $x = 1; // print the numbers from 1 to 10 while ($x <= 10) {   echo "$x<br/>\n"; } ?> 

simply forgetting to increment the loop counter $x in the previous code would cause it to enter an infinite loop from which PHP would not exit until it reached its maximum execution time (after which PHP would terminate script execution).

External Errors

Another source of errors in our scripts can be categorized as errors from external sources, such as the operating system, a database server, or the PHP functions we are using. Even a simple statement that you have executed thousands of times, such as the following

 <?php   $conn = new mysqli('localhost', 'db_user', 'db_passwd',                      'online_store'); ?> 

can suddenly fail for a number of reasons:

  • The username or password for the database has changed.

  • The database server has been moved to a different host.

  • The database server has crashed.

  • The disk on which the database is stored is having problems, and the database server is unable to fully establish a connection to it.

We could spend hours coming up with reasons why functions and classes could fail. Working with files in PHP (see Chapter 24, "Files and Directories") can succeed for a while and then suddenly fail because the hard disk filled up, the network file system disk disappeared due to a network problem, or somebody changed the permissions on a file.

Even simpler functions, such as the string functions mentioned in Chapter 6, "Strings and Characters of the World," can generate errors if the set of parameters or format of the parameters provided is incorrect.

You should remember that any function can fail for any number of reasons. Even though a particular object, function, or group of functions works fine for a while does not mean that it will not start to fail at some point in the future. Therefore, error checking is always necessary in our web applications.




Core Web Application Development With PHP And MYSQL
Core Web Application Development with PHP and MySQL
ISBN: 0131867164
EAN: 2147483647
Year: 2005
Pages: 255

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net