Error handling is the process to check errors in your program. In PHP it is very
easy to handle errors because in browser it display file name, line number and
small description about the error.
If you want to open or read a text file in php. If the file is missing then in your page/ browser it will display
Warning message. To avoid this you can use die() function.
<?php if(!file_exists("myfile.txt")) { die("File not found"); } else { $file=fopen("myfile.txt","r"); } ?>
error_level
: Required error_message
: Requirederror_file
: Optionalerror_line
: Optionalerror_context
: Optional [This parameter has been DEPRECATED as of PHP 7.2.0.]Value | Constant | Description |
2 | E_WARNING | Non-fatal run-time errors. Execution of the script is not halted |
8 | E_NOTICE | Run-time notices. The script found something that might be an error, but could also happen when running a script normally |
256 | E_USER_ERROR | Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error() |
512 | E_USER_WARNING | Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error() |
1024 | E_USER_NOTICE | User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error() |
4096 | E_RECOVERABLE_ERROR | Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler()) |
8191 | E_ALL | All errors and warnings (E_STRICT became a part of E_ALL in PHP 5.4) |
<?php //error handler function function customError($errno, $errmsg) { echo "<b>Error:</b> [$errno] $errmsg"; } //set error handler set_error_handler("customError"); //trigger error echo($myVar); ?>
The above code will display the following message.
Error: [8] Undefined variable: myVar
<?php # Turn off all error reporting error_reporting(0); # Report simple running errors error_reporting(E_ERROR | E_WARNING | E_PARSE); # Reporting E_NOTICE can be good too error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); # Report all errors except E_NOTICE # This is the default value set in php.ini error_reporting(E_ALL ^ E_NOTICE); # Turn off deprecated error error_reporting(E_ALL ^ E_DEPRECATED); # Report all PHP errors error_reporting(E_ALL); ?>