Often I have seen programmers getting troubled with PHP Errors. These are mostly coding / syntax related errors and they consume maximum amount of time to debug. Errors in PHP can happen at various occurrences.
These errors are categorized based on the time of occurrences, and based on whether it is recoverable or not. And then, this classification is made with respect to how it is triggered to send error message to the browser. It might be triggered automatically while executing improper line of code, or triggered by user by using trigger_error() function.
PHP errors will occur with some improper attempts with PHP scripts like, invalid line of code execution, infinite loop that cause default execution time elapse (30 seconds), and etc. Let us start with major classification of PHP errors as follows.
- Fatal error
- Parse error
This type of errors are uncaught exceptions that can not be recovered. When this error occurred, then it will stop execution. Based on the time of occurrence, fatal errors are classified as,
- Startup fatal error – This will be occur when the code cannot be executed with the PHP environment due to the fault that occurred at the time of installation.
- Compile time fatal error – This kind of error will occur when we attempt to use non existent data like file, class, function and etc.
- Run time fatal error – This will occur during execution. It is similar to compile time fatal error, except Compile time fatal error is generated by Zend engine based on the time of occurrence.
Example: PHP Fatal Error
Let us call a non existent function map() in the following PHP program.
echo "showing the map!"
This program will raise the following fatal error at the time of execution which will stop executing further line that is the echo statement.
Fatal error: Call to undefined function map() in ... on line 2
Parse errors are generated only at run time and are also known as syntax error. If anything goes wrong with PHP syntaxes, for example, missing semi colon for the end of line, PHP engine will generate an error which will be shown in the browser – example below:
echo "content to be displayed to the browser!"
echo "<br/>embedding line break";
This program send parse error to the browser as follows due to the lack of semicolon(;) at the end of line.
Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in ... on line 3
Like fatal errors, PHP warning messages also created based on the three types of warning, that is, Start up warning, Compile time warning and Run time warning. PHP will create warning message for sending them to the user without halting execution. Example scenario for warning messages to be created is the divide by zero problem that is as shown in the following PHP program.
$count = 0;
$total = 200;
$result = $total/$count;
echo "RESULT: ". $result;
In the above program, since $count has the value 0 and any number divide by zero is undefined, the line on which the division is made will create the following warning notices followed by the string returned by the echo statement with empty value for $result variable. Meaning that, even after the occurrence of the warning error, the echo statement is executed.
Warning: Division by zero in ... on line 4
Like other PHP error messages, notice message can be created automatically or by user by using PHP trigger_error() function.It is used to send messages to the browser to make user to know about the problem of the code is any, which might cause error.
For example, the following program starts with incrementing an uninitialized variable $result to print incremented value to the browser. Since, $result is not initialized, it will automatically trigger the notice error on executing this script.
$result += 1;
echo "RESULT: ". $result;
And the notice is,
Notice: Undefined variable: result in ... on line 2
But program execution will not be terminated because of this PHP notice. Rather, the notice message will be sent to the browser and the echo statement will print the incremented $result value subsequently.