🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

How to Fix PHP Error: A Guide on Handlers, Functions and Constants

Reading time 6 min
Published Aug 8, 2017
Updated Oct 2, 2019

You already learned how PHP error handling works, what PHP errors are in their essence, and how to work with them. Still, there is a lot of functions you don't know. Some of them (like PHP display errors) may come in handy often.

PHP error handling is not a task you'd want to put away for long. Therefore, in this tutorial, we will introduce you with various functions and constants that will prove helpful to you as you're using PHP error handlers and creating custom error logs.

PHP Error: Main Tips

  • PHP error functions are meant for dealing with error handling and logging tasks.
  • Error functions allow you to define how PHP errors will be handled and logged.
  • Logging errors means sending their messages to be saved in specified files, emails and other kind of system logs.
  • Error report functions let you customize how certain PHP errors are reacted to, and the level of feedback.
  • The functions are inbuilt into the PHP core.
  • This tutorial explains in detail what is a PHP error.

Settings for Runtime Configuration

The way these functions behave is affected by the settings located in php.ini:

Name Default Description Changeable
error_reporting NULL Set PHP error reporting level (either integer or named constants) PHP_INI_ALL
display_errors "1" Specify whether errors are to be printed to the screen, or if they would be hidden from the user.
Note: NEVER use this setting on production systems (only to support your development)
PHP_INI_ALL
display_startup_errors "0" Even if PHP display errors is on, errors which occur during PHP's startup sequence will not be displayed
Note: Keeping display_startup_errors off is recommended, except when debugging
PHP_INI_ALL
log_errors "0" Define which script error message should be logged to, the server error log or error_log.
Note: It is strongly advised to use error logging instead of error displaying on production web sites
PHP_INI_ALL
log_errors_max_len "1024" Used for setting maximum length of log_errors byte-wise. Value 0 can be used to apply no maximum length. This length is to apply to logged, displayed errors, as well as to $php_errormsg (implemented in PHP 4.3) PHP_INI_ALL
ignore_repeated_errors "0" Specify whether to log repeating error messages. If set to 1 it will stop logging errors that repeating errors in the same file on the same line (implemented in PHP 4.3) PHP_INI_ALL
ignore_repeated_source "0" Specify whether to log repeating error messages. If set to 1 it will stop logging errors with repeating errors from different files or source lines (implemented in PHP 4.3) PHP_INI_ALL
report_memleaks "1" When set to 1 (default value), this parameter shows a report of memory leaks that have been detected by the Zend memory manager (implemented in PHP 4.3) PHP_INI_ALL
track_errors "0" When set to 1, last error message always is present inside the variable $php_errormsg PHP_INI_ALL
html_errors "1" Turn off HTML tags in error messages PHP_INI_ALL
PHP_INI_SYSTEM in PHP <= 4.2.3.
xmlrpc_errors "0" Turn off normal PHP error reporting and start formatting errors as XML-RPC error messages (implemented in PHP 4.1) PHP_INI_SYSTEM
xmlrpc_error_number "0" Used as value of XML-RPC faultCode element (implemented in PHP 4.1) PHP_INI_ALL
docref_root "" (implemented in PHP 4.3) PHP_INI_ALL
docref_ext "" (implemented in PHP 4.3.2) PHP_INI_ALL
error_prepend_string NULL Specify string to output before error message PHP_INI_ALL
error_append_string NULL Specify string to output after error message PHP_INI_ALL
error_log NULL Specify the name of file where script errors are to be logged. File should be set to be writable by web server's user. If special value syslog is used, errors are sent to system logger instead PHP_INI_ALL
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

List of Functions to Handle Errors

For the PHP error management, you are welcome to use the following functions. The table below indicates the most common methods that might be applied to get information on the latest errors, set different handler functions and perform other related actions:

Function Description
debug_backtrace() Generate backtrace
debug_print_backtrace() Print backtrace
error_get_last() Return last error that occurred
error_log() Send error message to log, to file, or to mail account
error_reporting() Specify which errors are reported
restore_error_handler() Restore previous PHP error handler
restore_exception_handler() Restore previous exception handler
set_error_handler() Set user-defined PHP error handler function
set_exception_handler() Set user-defined exception handler function
trigger_error() Create user-level error message
user_error() Alias of trigger_error() function

Relevant Predefined Constants

In PHP error handling, you can use the predefined constants that are designed for allowing you to set which errors are supposed to be displayed. Take a look at the following table and learn the meaning of each constant:

Value Constant Description
1 E_ERROR Fatal run-time error, which cannot be recovered from, halts script execution.
2 E_WARNING Minor, non-fatal error, which occur while running the script and do not stop it.
4 E_PARSE Compile-time errors, generated by the parser.
8 E_NOTICE This include run-time notices, which could include minor errors as well as notices you may get when normally running the script.
16 E_CORE_ERROR Fatal errors PHP startup error, much like E_ERROR, only that generated by PHP core.
32 E_CORE_WARNING Non-fatal PHP startup errors, much like E_WARNING, only that generated by PHP core.
64 E_COMPILE_ERROR Fatal errors. generated during compile-time, much like E_ERROR only that generated by Zend Scripting Engine.
128 E_COMPILE_WARNING Non-fatal errors, generated during compile-time, much like E_WARNING only thatgenerated by Zend Scripting Engine.
256 E_USER_ERROR Fatal error, user-generated error, a lot like E_ERROR only that they are set by code author using trigger_error() function.
512 E_USER_WARNING Non-fatal user-generated warnings, a lot like E_WARNING only that they are set by code author using trigger_error() function.
1024 E_USER_NOTICE Notice, user-generated, a lot like E_NOTICE only that they are set by code author using trigger_error() function.
2048 E_STRICT Lets PHP suggest changes for your code that will ensure the most optimal interoperability and forward compatibility for your code (Since PHP 5 but only got included in E_ALL in PHP 5.4)
4096 E_RECOVERABLE_ERROR Fatal error, which can be caught, a lot like E_ERROR , but it can be caught using a user-defined PHP error handler, that can be set up using set_error_handler().
8192 E_DEPRECATED All existing errors, warnings and notices (E_STRICT became a part of E_ALL in PHP version 5.4)
16384 E_USER_DEPRECATED Warning message, generated by the user, much like E_DEPRECATED, only that it is generated in PHP code using PHP function trigger_error() (Since PHP 5.3)
32767 E_ALL Enable every available PHP error and warning (except for E_STRICT in versions earlier than 5.4)

PHP Error: Summary

  • To handle and log errors, you should use special PHP error functions.
  • When an error is logged, their messages are sent and saved in a log, such as a specified file or email.
  • Using PHP error reporting, you can define system's feedback to particular PHP errors and its level.
  • These functions don't require special installation.
  • To remember what is a PHP error, visit this tutorial.