This page is about the standard error handling system of php known also as the error functions. 1)
This system:
Note that this system is the system used by all built-in function. For your own, code php supports now error handling via exception
To trigger an error in your code the same way, you would use the trigger-error function (trigger-error). This trigger creates an error object that is send to the callback
if ($divisor == 0) {
trigger_error("Cannot divide by zero", E_USER_ERROR);
}
// or
\trigger_error('foo', \E_USER_NOTICE);
\trigger_error('foo', \E_USER_DEPRECATED);
\trigger_error('foo', \E_USER_WARNING);
\trigger_error('foo', \E_USER_ERROR);
The callback function is a callback function that catches all error send by the trigger function
By default, the php callback is called the standard PHP error handler.
It will catch all error and makes them available via the error_get_last function 3)
$error_get_last = error_get_last();
To configure your own callback, you would use the set-error-handler (set-error-handler))
Example:
if ($debug) {
set_error_handler ('debug_error_handler');
} else {
set_error_handler ('nice_error_handler');
}
In a single value, you can may have multiple error level (ie error but warning with notice)
The value is encoded on 16 bit where each bit set the particular level on or off
Example:
The table below gives you the bit value of a couple of level. The whole table can be seen at the errorfunc.constants page.
Level | Description | Bit | Integer |
---|---|---|---|
E_ERROR | First Bit | 0000000000000001 | 1 |
E_WARNING | Second Bit | 0000000000000010 | 2 |
E_PARSE | Third Bit | 0000000000000100 | 4 |
E_NOTICE | Fourth Bit | 0000000000001000 | 8 |
and so on until …. | |||
E_ALL | All bit | 0111111111111111 | 32767 |
Because the message level is set on a binary system number, you may then use bitwise operator to set each bit and choses the level of the messages that you want to report. Example: To show all errors, except for notices, the php.ini file instructions may set the following with the bitwise AND (&) and NOT (~) operator
E_ALL & ~E_NOTICE = 0111111111110111
You can set the the error level to report:
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
\error_reporting()
Example with the value 32,759, the 4th bit is unset and that means that the E_NOTICE message are not shown.
How to catch a php error (E_WARNING or E_ERROR) ?
if (!($errorLevelNumber & \error_reporting())) {
return false; // Don't show
} else {
return false; // Show
}
You can get the last error with the error-get-last function 5)
$error_get_last = error_get_last();
You can delete the last error with the error-clean-last function 6)
error_clear_last();
PHP supports one error control operator: the at sign (@) 7)
When used as prefix on an expression, any error that might be generated will be suppressed.
Example:
$foo = @functionName();
# with a standard function such as fopen, no error will be triggered if the file is not known / https is not set, ...
$filePointer = @fopen($downloadUrl, 'r');