How can I display (echo/print) the currently set error reporting level in PHP?

Trevor picture Trevor · Feb 6, 2012 · Viewed 14.6k times · Source

I am working on a rather large project (multiple teams) so I don't have complete control over the code. Unfortunately, error_reporting is changed in many places throughout the code. When I get to a certain point in the code, I want to see what error reporting is currently set to. Is there anyway to accomplish this?

Answer

Cheery picture Cheery · Feb 6, 2012

http://www.php.net/error_reporting

int error_reporting ([ int $level ] )

Returns the old error_reporting level or the current level if no level parameter is given.

You could also use examples provided by the link in order to cast the level (which is returned as integer) into the string. For example:

function error_level_tostring($intval, $separator = ',')
{
    $errorlevels = array(
        E_ALL => 'E_ALL',
        E_USER_DEPRECATED => 'E_USER_DEPRECATED',
        E_DEPRECATED => 'E_DEPRECATED',
        E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
        E_STRICT => 'E_STRICT',
        E_USER_NOTICE => 'E_USER_NOTICE',
        E_USER_WARNING => 'E_USER_WARNING',
        E_USER_ERROR => 'E_USER_ERROR',
        E_COMPILE_WARNING => 'E_COMPILE_WARNING',
        E_COMPILE_ERROR => 'E_COMPILE_ERROR',
        E_CORE_WARNING => 'E_CORE_WARNING',
        E_CORE_ERROR => 'E_CORE_ERROR',
        E_NOTICE => 'E_NOTICE',
        E_PARSE => 'E_PARSE',
        E_WARNING => 'E_WARNING',
        E_ERROR => 'E_ERROR');
    $result = '';
    foreach($errorlevels as $number => $name)
    {
        if (($intval & $number) == $number) {
            $result .= ($result != '' ? $separator : '').$name; }
    }
    return $result;
}

use it as echo error_level_tostring(error_reporting(), ',');