Subscribe For Free

Thursday, March 8, 2018

PHP Errors

Different kind of error events may occur in PHP. These errors are classified based on the time of occurrences and whether it is recoverable or not. And this classification is made with respect to how it is triggered to send an error message to the browser. It might be triggered automatically while executing an improper line of code.
  1. Fatal error
  2. Parse error
  3. Warning
  4. Notices

How to enable errors in PHP
ini_set('display_errors', '1');

Fatal Error

In computing, a fatal error or fatal exception error is an error that causes a program to abort and may, therefore, return the user to the operating system. When this happens, data that the program was processing may be lost. fatal errors are classified as,
  • Startup fatal error – This will 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 nonexistent data like file, class, function and etc.
  • Runtime fatal error – This will occur during execution. It is similar to compile time fatal error, except Compile time fatal error is generated by the Zend engine based on the time of occurrence.

Example: PHP Fatal Error

Let us call a non-existing function myFunction() in the following PHP program.

echo "!"
This program will raise the following fatal error at the time of execution which will stop executing a further line that is the echo statement.

( ! ) Fatal error: Call to undefined function myFunction() in C:\xampp\htdocs\demo\today\demo.php on line 2
Call Stack
10.0010130016{main}( )...\demo.php:0

Parse Error

Parse errors are generated only at compile time which is also called as a syntax error. If anything wrong with PHP syntax, for example, missing semi-colon for the end of the line, will trigger this type of errors to be displayed to the browser.

echo ""
echo "<br/>error";
This program sends parse error to the browser as follows due to the lack of semicolon(;) at the end of the line.

( ! ) Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in C:\xampp\htdocs\demo\today\demo.php on line 3


PHP will create warning message for sending them to the user without halting the execution. An example for warning messages to be created is the divide by zero problem that is as shown in the following program.

$numberOne = 0;
$NumberTwo = 10;
$result = $NumberTwo/$numberOne;
echo $result;
In the above program, since $numberOne has the value 0 and any number divided 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 an 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 C:\xampp\htdocs\demo\today\demo.php on line 4
Call Stack
10.0000129832{main}( )...\demo.php:0


Like other PHP error messages, notice message can be created automatically or by the user by using the PHP trigger_error() function.It is used to send messages to the browser to make the user know about the problem of the code is any, which might cause an error.

For example, the following program starts with incrementing an uninitialized variable $result to print the incremented value to the browser. Since $result is not initialized, it will automatically trigger the notice error on executing this script.

$result += 5;
echo "RESULT: ". $result;
( ! ) Notice: Undefined variable: result in C:\xampp\htdocs\demo\today\demo.php on line 2
Call Stack
10.0000129672{main}( )...\demo.php:0


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.


E_ERROR: A fatal error that causes script termination
E_WARNING: Run-time warning that does not cause script termination
E_PARSE: Compile time parse error.
E_NOTICE: Run time notice caused due to error in code
E_CORE_ERROR: Fatal errors that occur during PHP’s initial startup (installation)
E_CORE_WARNING: Warnings that occur during PHP’s initial startup
E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
E_USER_ERROR: User-generated error message.
E_USER_WARNING: User-generated warning message.
E_USER_NOTICE: User-generated notice message.
.E_STRICT: Run-time notices.
E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
E_ALL: Catches all errors and warnings

click it