Common Errors

I l @ ve RuBoard

The most common type of error you'll run across is syntacticalfailing to cross all your t's and dot all your i's, so to speak. Errors like this will result in error messages like the one in Figure 14.1. These are the first types of errors PHP will catch because the code is evaluated for syntax before execution takes place. In order to avoid making these sorts of mistakes when you program, be sure to:

Figure 14.1. By now you've certainly seen your fair share of parse errors generated by syntactical mistakes in your scripts.

graphics/14fig01.gif

  • End every executable line of code with a semi- colon .

  • Use a closing quotation mark, parentheses, bracket, or brace for every opening quotation mark, parentheses, bracket , or brace.

  • Escape, using the backslash, all single- and double-quotation marks within the print() statement.

One thing you should also understand about syntactical errors is that just because the PHP error message says the error is occurring on line 12, for example, doesn't mean that the mistake is actually on that line. It is not uncommon for there to be a difference between what PHP thinks is line 12 and what your text editor indicates is line 12. So while PHP's direction is useful in tracking down a problem, learn to treat the line number referenced as more of a starting point rather than an absolute.

The second type of error you'll encounter results from trying to do something which cannot be done. These errors occur, for example, when setcookie() or header() is called after the Web browser has already received HTML, when a function is called without the proper arguments, or when you try to write to a file that does not have the proper permissions. These errors are discovered by PHP when attempting to execute the code (Figure 14.2).

Figure 14.2. Warnings are created by PHP upon attempting to execute the script and are generally caused by misuse of or mis-reference to functions (e.g., misspelling a function's name or using the wrong number of arguments when calling a function).

graphics/14fig02.gif

The third type of error you'll seeall too frequentlyaren't PHP errors at all, but programmer errors, also called logic errors. (Some programmers would argue that all errors are programmer errors because PHP itself cannot make such mistakes.) One reason such errors happen is because you fail to use the proper variable name. If you do that, you will not see an error message like those in Figures 14.1 and 14.2, you will just witness odd or unpredictable results. These can be the hardest to discern because PHP will not give you a clue as to what the problem is or how to solve it. Nothing but accurate checking and smart detective work will fix them!

For the first exercise, let's add a safety check to the HandleLogin.php script from Chapter 13, Creating Web Applications, to avoid the commonplace occurrence of sending a header after the Web browser has already received some HTML or blank space.

To prevent a common error:

  1. Open HandleLogin.php in your text editor (Script 14.1).

    Script 14.1. The original HandleLogin.php page was certainly acceptable but could be improved upon by making use of the headers_sent() function.

    graphics/14sc01.gif

  2. Create a blank line by pressing Return before the initial PHP tag (Script 14.2).

    Script 14.2. The headers_sent() function helps to avoid the common mistake of sending a header (or a cookie) after the Web browser has already received information (Figure 14.3).

    graphics/14sc02.jpg

    This blank line, while it may seem insignificant, will generate the error message seen in Figure 14.3.

    Figure 14.3. Attempting to set a cookie or send a header after the Web browser has already received any information creates an error like this.

    graphics/14fig03.gif

  3. After the initial PHP tag, add the following conditional:

     if ( headers_sent() ) {      print ("Cannot process your request     due to a system error!\n"); } else { 

    The headers_sent() function returns the value TRUE if any HTML, blank space, etc. has been sent to the Web browser already. If this has occurred, attempting to use the header() function will create a harsh warning in the user 's browser. Instead of having that happen, if the headers have been sent, this page will now show a generic system error.

    If the headers have not been sent, then the headers_sent() function will be false and the rest of the page will be executed as normal.

  4. Don't forget to close the headers_sent() conditional before the closing PHP tag!

     } 
  5. Save your script as HandleLogin.php, upload it to the server in the same directory as login.php, and test both pages in your Web browser (Figure 14.4).

    Figure 14.4. By using the headers_sent() function, you can print a less perplexing message to the user. It still doesn't solve the problem, but it is better than having users see messages like that in Figure 14.3.

    graphics/14fig04.gif

Tip

Some text editors, such as BBEdit for the Macintosh, include utilities to check for balanced parenthesis, brackets, and quotation marks.


Table 14.1.
Error Reporting Levels
Number Constant Meaning
1 E_ERROR Fatal run-time
2 E_WARNING Non-fatal run-time warnings
4 E_PARSE Compile-time parse
8 E_NOTICE Run-time notices
16 E_CORE_ERROR fatal errors that occur during PHP's initial startup (PHP 4 only)
32 E_CORE_WARNING warnings (non fatal errors) that occur during PHP's initial startup (PHP 4 only)
64 E_COMPILE_ERROR fatal compile-time errors (PHP 4 only)
128 E_COMPILE_WARNING compile-time warnings (non fatal errors) (PHP 4 only)
256 E_USER_ERROR user-generated error message (PHP 4 only)
512 E_USER_WARNING user-generated warning message (PHP 4 only)
1024 E_USER_NOTICE user-generated notice message (PHP 4 only)
  E_ALL all of the above, as supported
I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

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