PHP E_ERROR

An E_ERROR type is unrecoverable (fetal) and causes the script to stop further execution. Fetal errors can happen due to syntax errors.

1681 views
d

By. Jacob

Edited: 2020-02-24 06:24

E_ERROR, PHP tutorial.

The E_ERROR represents fetal errors in PHP, and might happen whenever an error occurs that is not recoverable.

The E_ERROR type can not be handled by a Custom Error Handler, and will instead be passed on to PHP's build-in error handling features.

The level of error handling can be controlled in the configuration files, or you can control it on a per-script basis with the error_reporting function.

To only report fetal errors (E_ERROR), you may place the following somewhere near the top of a script:

error_reporting(E_ERROR);

You can combine this with other error types using the pipe character |. To only show simple running errors, use the below:

error_reporting(E_ERROR | E_WARNING | E_PARSE);

Causes for E_ERROR

An E_ERROR type covers fetal run-time errors, and might be caused by many different problems. To find out what caused the problem, be sure to examine the server log files.

One cause, common when using type hinting, might be worded like this:

Uncaught TypeError: Argument 1 passed to my_function() must be of the type string, int given, called in ...

This paticular error means that the first argument passed to the my_function function was of the wrong data type. A string was given, but it the function only works with an integer.

It can also be caused by syntax errors in the code, in which case the exact line number would usually be indicated in the error log:

PHP Parse error: syntax error, unexpected ')' in ... on line 22

Finally, a missing semicolon is also a common cause for parse errors:

PHP Parse error: syntax error, unexpected end of file in ...

Custom error messages

E_ERROR should not be used with the trigger_error function to show custom error messages. Instead, consider using E_USER_ERROR type, which is meant for user-made error messages:

trigger_error("Sorry friend. Something has prevented the script from running.", E_USER_ERROR);

Note. Only E_USER* types should be used with trigger_error.

Debugging error messages

Often when a fetal run-time error (E_ERROR) occurs, you will get either a blank page or a 500 Internal Server Error response, even when errors is turned on. This is not very helpful when trying to debug a problem, however, the error will usually be logged in the server error log.

The location of the error log depends on your system. It is often located in /var/log/apache2/example.com-error.log

Note. Replace example.com with your own VHOST log name.

Once you have located the log file, you can read the last line with the tail command in Linux:

tail -n 1 /var/log/apache2/example-error.log

Change the number after the -n parameter to adjust the number of lines you want returned.

Tell us what you think:

  1. In this Tutorial, it is shown how to redirect all HTTP requests to a index.php file using htaccess or Apache configuration files.
  2. How to create a router in PHP to handle different request types, paths, and request parameters.
  3. Tutorial on how to use proxy servers with cURL and PHP
  4. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.
  5. How to effectively use variables within strings to insert bits of data where needed.

More in: PHP Tutorials