PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
In PHP, different types of errors can be generated during script execution. These errors can be categorized into various levels. Understanding and controlling these error levels is essential for effective debugging and error handling in your PHP applications.
Here's a brief tutorial on PHP error levels:
Types of Error Levels
E_ERROR
: A fatal run-time error that can't be recovered from. The script execution is halted.
E_WARNING
: A run-time warning. It's non-fatal and most likely won't halt script execution.
E_PARSE
: Compile-time parse error. Parse errors should only be generated by the parser.
E_NOTICE
: A run-time notice indicating that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
E_CORE_ERROR
: Fatal errors that occur during PHP's initial startup (PHP core).
E_CORE_WARNING
: Warnings (non-fatal errors) that occur during PHP's initial startup.
E_COMPILE_ERROR
: Fatal compile-time errors indication problem with script.
E_COMPILE_WARNING
: Compile-time warnings (non-fatal errors).
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 occurred.
E_DEPRECATED
: Run-time notices. Enable this to receive warnings about code that will not work in future versions.
E_USER_DEPRECATED
: User-generated warning message.
E_ALL
: All errors and warnings (except level E_STRICT
in versions less than PHP 5.4)
Setting Error Levels
You can set the level of error reporting using the error_reporting()
function in your PHP script:
<?php // Report all errors error_reporting(E_ALL); // Report all errors except E_NOTICE error_reporting(E_ALL & ~E_NOTICE); ?>
You can also set the error level in the php.ini file:
; Report all errors error_reporting = E_ALL ; Report all errors except E_NOTICE error_reporting = E_ALL & ~E_NOTICE
Displaying Errors
By default, PHP sends an error log to the server's logging system or a file, depending on how the error_log configuration is set in the php.ini file. You can tell PHP to display error messages directly in the browser by changing the display_errors directive in the php.ini file, or in your script like this:
<?php // To display error messages ini_set('display_errors', 1); // To hide error messages ini_set('display_errors', 0); ?>
It is generally recommended to set display_errors = 0
in the php.ini file when your script is running on a production server to avoid exposing potential sensitive information.
Logging Errors
If you want to log errors instead of or in addition to displaying them, you can use the log_errors
directive in the php.ini file or in your script:
<?php // To log errors ini_set('log_errors', 1); // To stop logging errors ini_set('log_errors', 0); ?>
These are the basics of PHP error levels. The best practice is to have error reporting turned on during development to understand and fix issues in the code, and then turn off display errors when moving to production for security and user experience.
PHP error levels hierarchy:
The error levels have a hierarchy, with E_ALL
being the highest and E_ERROR
being the most severe. As you go down the hierarchy, the severity of errors decreases.
How to set error reporting level in PHP:
Set the error_reporting level using the error_reporting()
function. Example:
<?php // Set error_reporting to report all errors error_reporting(E_ALL);
Displaying errors in PHP with error_reporting()
:
Display errors by setting the error_reporting level:
<?php // Display all errors error_reporting(E_ALL); ini_set('display_errors', 1);
PHP error_reporting()
for development and production:
For development, it's common to show all errors, while for production, you may want to log errors without displaying them:
<?php // For development error_reporting(E_ALL); ini_set('display_errors', 1); // For production error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', 0);
Logging errors with PHP error_reporting()
:
Log errors using the error_log()
function and setting the error_reporting level:
<?php // Log errors to a file error_reporting(E_ALL); ini_set('log_errors', 1); ini_set('error_log', 'error.log');
PHP error_reporting()
and error_log()
functions:
Use both error_reporting()
and error_log()
functions for comprehensive error handling:
<?php // Set error_reporting level error_reporting(E_ALL); // Log errors to a file ini_set('log_errors', 1); ini_set('error_log', 'error.log');
PHP ini_set()
for dynamically changing error_reporting level:
Dynamically change error_reporting using ini_set()
:
<?php // Set error_reporting level dynamically ini_set('error_reporting', E_ALL);
PHP error_reporting(E_ALL)
meaning:
error_reporting(E_ALL)
means that all error types will be reported, including notices, warnings, and fatal errors.
Suppressing errors with PHP @
symbol:
Use the @
symbol to suppress errors for a specific line of code:
<?php // Suppress errors for this line $result = @file_get_contents('nonexistent_file.txt');
PHP error_reporting()
for handling strict standards:
Adjust error_reporting to handle strict standards:
<?php // Handle strict standards error_reporting(E_ALL | E_STRICT);
PHP error_reporting()
and E_STRICT
:
Include E_STRICT
in the error_reporting level to handle strict standards:
<?php // Set error_reporting to handle strict standards error_reporting(E_ALL | E_STRICT);
Setting error_reporting
in PHP CLI:
When running PHP from the command line (CLI), set error_reporting
in the PHP command:
php -d error_reporting=E_ALL script.php