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.
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).
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:
Tip Some text editors, such as BBEdit for the Macintosh, include utilities to check for balanced parenthesis, brackets, and quotation marks. Table 14.1.
|
I l @ ve RuBoard |