Php - Error

Card Puncher Data Processing


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);
  • Standard php built-in function uses this system and therefore uses this trigger function internally.
  • You can also send an error to the web server's error log or to a file with the error_log function 2)


The callback function is a callback function that catches all error send by the trigger function

What is the default callback function (ie standard PHP error handler) ?

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();

How to configure the callback function ?

To configure your own callback, you would use the set-error-handler (set-error-handler))


if ($debug) {
    set_error_handler ('debug_error_handler');
} else {
    set_error_handler ('nice_error_handler');


What is the level value ?

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


  • If the first bit is set to 1, we got all messages with an E_ERROR level.
  • If the first and second bit is set to 1, we got all messages with an E_ERROR && E_WARNING level.
  • and so on.

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

How to set a level ?

You can set the the error level to report:

  • in the php.ini file
  • or in your code (at runtime) by using the error_reporting function 4)
ini_set('display_errors', true);
ini_set('html_errors', false);

How to known which level is set

  • And check the bits:
    • 0 means the message will not be shown at this level
    • 1 means the message will be shown at this level

Example with the value 32,759, the 4th bit is unset and that means that the E_NOTICE message are not shown.

Php Error Level Enotice Not Set

How to takes over the error system by throwing an exception ?

How to catch a php error (E_WARNING or E_ERROR) ?

How to test if you need to show a message

if (!($errorLevelNumber & \error_reporting())) {
    return false; // Don't show
} else {
   return false; // Show

Last error

How to get the last error ?

You can get the last error with the error-get-last function 5)

$error_get_last = error_get_last();

How to clean/delete the last error ?

You can delete the last error with the error-clean-last function 6)


How to suppress the error for a function ?

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.


$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');

Discover More
Card Puncher Data Processing
How to catch a php error (E_WARNING or E_ERROR) ?

This howto shows you how to catch an php error. The general procedure is to: create a exception to throw when an error happens replace the general error handling function with our own that throw...
Card Puncher Data Processing
Php - Exception

This page is the Exception feature of Php. error reporting feature The basic structure is: where: catch will run if there is an exception thrown finally will run at the end of the try/catch...

Share this page:
Follow us:
Task Runner