How do I get PHP errors to display. In this tutorial, you will learn how to display all errors in PHP.
And as well as learn, how to reporting all errors in PHP.
First of all, you need to know that in PHP, Some error prevents the execution of the script. And some errors only display error messages with warning. Then you will learn how to show or hide all errors reporting in PHP.
There are four types of errors in PHP are:
- Notice Error
- Warning Error
- Fatal Error
- Parse Error
When trying to access the undefined variable in PHP script at that time notice error occurs. Notice error does not stop the execution of PHP script. It only notice you that there is a problem.
The following example represents the notice error:
In this example, define only $a variable and print it. As well as print $b variable without define. When executing this script, a notice error occurs and display a message, the $b variable is not defined.
The main reason to occurs warning errors is to include a missing file, calling a function with incorrect parameters. It is similar to notice an error, it also does not stop the execution of PHP script. It only reminds you that there is a problem in the script.
The following example represents the warning error:
echo "Warning error"';
In this example, include any PHP file that does not exist in your application. At that time, it displays a message that failed to include it.
When call to undefined functions and classes in PHP script, at that time fatal error occurs. The Fatal error does stop the execution of the PHP script and it display error message.
Note that, if you call the undefined function or class in the script is the main reason to occurs fatal error.
The following example represents the fatal error:
$a = 10;
$b = 50;
$c = $a + $b;
echo "Addition of a + b = ".$c;
In this example, A function is defined, which name is addition(). This function adds 2 variables and prints its result.
When the addition() function is called. After that, sub() function is also called which is not defined. At this time, the fatal error comes and stops the execution of the script with an error message.
The parse error happens, when a syntax mistyped in the script. Parse error does also stops the execution of the PHP script and it display error message.
The following main reason for syntax/parse errors are:
- Unclosed quotes
- Missing or Extra parentheses
- Unclosed braces
- Missing semicolon
The following example represents the parse error:
In this example, print a 1, 2, 3. After print “1”, you missed a semicolon. At that time it occurs.
Suddenly Show All PHP Errors
To display all php errors and warnings is to add these lines to your PHP code file:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
Configure PHP.ini to Display All Errors
If adding some of the PHP code errors doesn’t show in the browser during testing, then the PHP ini configuration has some additional directives to handle this.
display_errors = on
Configuration .htaccess To Display All PHP Errors
Developers usually have access to the directory files. The directive for showing PHP errors can also be enabled or disabled using the .htaccess file located in the root or public directory of the project.
php_flag display_startup_errors on php_flag display_errors on