PHP Errors
PHP throws a different type of errors if something inappropriate happens during the program execution. It could be because of wrong syntax or many other reasons.
By default, PHP handles errors in a simple way. An error message is displayed to the browser with the filename, line number and a description of the error.
Types of PHP Errors
There are four basic types of run-time errors in PHP:
- Notices: These are small, non-critical errors that PHP encounters while executing a script. For example, if you are accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all.
- Warnings: These are some serious errors – for example, if you try to include a file using include() function and file does not exist at given path. By default, these errors are displayed to the user, but they do not terminate PHP script and rest of the program executes.
- Fatal errors: These are critical errors in php. For example, calling a function that does not exist. These errors immediately terminate the php script’s execution. By default these are visible to the users in browser.
- Parse Error: These errors also terminate the php script execution immediately. Beginners encounter these errors often, when there is mistake in PHP code/syntax. For example, when we miss semicolon or does not close brackets properly.
Error Reporting:
The error_reporting()
function sets the php error reporting. PHP has many levels of errors, using this function you can sets that level of your php script. We can turn on or off different type of errors as per our requirement.
Here are various examples to enable/disable errors in PHP:
Note: It is good to define error_reporting()
function at the top of the script. You can use different level of error reporting on different pages by adding above functions in each php script.
<?php
// Turn off all error reporting
error_reporting(0);
// Report all PHP errors
error_reporting(-1);
// Report all PHP errors
error_reporting(E_ALL);
// Report simple running errors (but no Notices)
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Report Notice and Warning only
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
?>
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
What are the different types of error in PHP? |
How you can force maximum execution time error in PHP? |