Combating Coding Errors - PHP Specific Error Messages and Reporting (Page 3 of 4 )
PHP error messages, in most cases, are fairly user-friendly. Let's take a look at a few error messages and the code that produced them.
Error: Fatal error: Call to undefined function: includ() in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 12
Comment: Since includ() is an undefined function, a fatal error (or a semantic error) occurs and execution stops. As you can see, PHP catches the error and details the problem (undefined function: includ()) and even provides the line that the error occurred on.
Since fatal errors are semantic errors, the script does execute up until the error. However, since PHP needs to compile and execute each line of code to continue, it will immediately stop upon encountering a fatal error.
Code: $x = 1 echo $x;
Error: Parse error: parse error in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 13
Comment: Using our syntax error example from above, the compiler did indeed catch the missing semicolon in our first statement ($x = 1). Although the error does not specifically state "missing semicolon", a parse error tells the programmer a syntax error has occurred, which allows the programmer to hone his or her debugging skills to a specific type of error.
Remember that since parse errors are syntax errors, no execution was performed on the script. Once the semicolon is added to the first line of code, the script will execute successfully and as intended.
Code: // $x was never declared echo $x;
Error: Warning: Undefined variable: x in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 12
Comment: As you can see, this is a warning, not an error (in this case, the warning message is also known as a notice). Warnings occur when PHP determines something is wrong within the script but is not serious enough to cease execution of the script.
It is essential to understand that warnings do not prevent the execution of the script. When a warning is encountered, a warning message will be placed within the output of a script where the line of code was read. PHP will continue executing the remainder of the script until the end of line is reached.
For example, the following code:
$i = 1; echo $x; echo $i;
Will produce this output:
Warning: Undefined variable: x in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 12 1
Notice the 1 at the end of the output. After PHP encountered the warning on the second executable line ($x not defined), it correctly interpreted and executed the third line.
Using error_reporting() The three examples above detail the types of error messages present within PHP. In an effort to give the programmer absolute control, PHP allows the programmer the ability to select which errors and warnings to display. If the programmer has access to the PHP.INI file on the host machine, errors can be globally modified. If, however, the programmer wishes to modify output relative to specific files, the error_reporting() function can be used instead.
The error_reporting() function takes one parameter that signifies the type of error/warning output to support and is usually written at the top of any PHP document. The error reporting parameters are as follows:
0: No error reporting
1: Fatal errors
4: Parse errors
So, if you'd rather only display fatal errors, then the function code looks like this:
What if the programmer wants fatal errors and parse errors together? Simply add the two values together.