How to display errors on php web pages?

To debug .php web pages, you want to print something on the browser’s windows when opening the web pages. You can use the php echo or print function to display debug information. But this is only feasible when you know the exact location where you print the information. If you do not know the exact position of the to be investigated problem, how to locate the bug?

<?php
$a=10/0;

The above code contains a divide by zero error. But if you open the web page in the browser, you can not see anything on the page. It is just a blank page. You may know the php function error_reporting. What if I use this function to let php report all errors?

<?php
error_reporting(E_ALL);

$a=10/0;

Unfortunately, you still get an empty page. The error_reporting function is equivalent to setting the initial variable “error_reporting” to E_ALL. So the following code does not work, either:

<?php
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
$a=10/0;

In fact, you need to set the initial variable display_errors to 1 to report this error:

<?php
ini_set('display_errors', 1);

$a=10/0;

You do not need to call error_reporting or set the ‘error_reporting’ initial variable because divide by zero is an E_WARNING error and the default report level covers this kind of error:

<?php
$errLvl = error_reporting();
for ($i = 0; $i < 15;  $i++ ) {
    print FriendlyErrorType($errLvl & pow(2, $i)) . "<br>\\n";
}

function FriendlyErrorType($type)
{
    switch($type)
    {
        case E_ERROR: // 1 //
            return 'E_ERROR';
        case E_WARNING: // 2 //
            return 'E_WARNING';
        case E_PARSE: // 4 //
            return 'E_PARSE';
        case E_NOTICE: // 8 //
            return 'E_NOTICE';
        case E_CORE_ERROR: // 16 //
            return 'E_CORE_ERROR';
        case E_CORE_WARNING: // 32 //
            return 'E_CORE_WARNING';
        case E_COMPILE_ERROR: // 64 //
            return 'E_COMPILE_ERROR';
        case E_COMPILE_WARNING: // 128 //
            return 'E_COMPILE_WARNING';
        case E_USER_ERROR: // 256 //
            return 'E_USER_ERROR';
        case E_USER_WARNING: // 512 //
            return 'E_USER_WARNING';
        case E_USER_NOTICE: // 1024 //
            return 'E_USER_NOTICE';
        case E_STRICT: // 2048 //
            return 'E_STRICT';
        case E_RECOVERABLE_ERROR: // 4096 //
            return 'E_RECOVERABLE_ERROR';
        case E_DEPRECATED: // 8192 //
            return 'E_DEPRECATED';
        case E_USER_DEPRECATED: // 16384 //
            return 'E_USER_DEPRECATED';
    }
    return "";
} 

ini_set('display_errors', 1);

$a=10/0;

The above code produces the following output:

E_ERROR
E_WARNING
E_PARSE
E_NOTICE
E_CORE_ERROR
E_CORE_WARNING
E_COMPILE_ERROR
E_COMPILE_WARNING
E_USER_ERROR
E_USER_WARNING
E_USER_NOTICE

E_RECOVERABLE_ERROR

E_USER_DEPRECATED

Warning: Division by zero in test.php on line 49

You can see what the default error reporting level is. Since the default error reporting level includes E_WARNING, it will display the divide by zero error. The E_WARNING error is such kind of error that does not stop the execution of the script, i.e.,after displaying the error on execution of the division statement, it continues to execute the following statements.

Now let’s consider another type of error:

<?php
ini_set('display_errors', 1);

a=10/0;

The above code contains a syntax error: the $ before the variable name a is missing. If you open the web page in your browser, the blank web page comes back again: no error reported. This is very annoying because most of the bugs on php pages at development time are this kind of syntax errors. And it does not give you any hint about the error. We know that the default error reporting level also covers “E_PARSE” which deals with syntax errors. Why does not it report the syntax error? Even adding error_reporting(E_ALL) before the error line does not work.¬† This is because syntax error is a kind of severe error. The script does not execute at all if there is a syntax error in it. So the init_set or error_reporting statement in the script does not even have the chance to execute. You need to tell the php engine to display errors before executing the script. How to do that? You should edit /etc/php.ini and change the line “display_errors = Off” to “display_errors = On”. Of course you should make sure the error reporting level in /etc/php.ini includes E_PARSING (which does by default) in order to report this kind of syntax errors. Including¬† E_PARSING using error_reporting function or init_set in script does not work because the error is caught before the script is executed. The syntax error looks like:
Parse error: syntax error, unexpected ‘=’ in test.php on line 49

reference: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display

Posted in

Comments are closed, but trackbacks and pingbacks are open.