预定义常量

下列常量作为 PHP 核心的一部分总是可用的。

注意: 你可以使用它们在 php.ini 中的常量名称; 但是在 PHP 之外,例如在 httpd.conf 之中, 你必须使用二进制位掩码来代替。

错误和日志记录
常量 说明 备注
1 E_ERROR (int) 致命的运行时错误。这类错误一般是不可恢复的情况,例如内存分配导致的问题。后果是导致脚本终止不再继续运行。  
2 E_WARNING (int) 运行时警告 (非致命错误)。仅给出提示信息,但是脚本不会终止运行。  
4 E_PARSE (int) 编译时语法解析错误。解析错误仅仅由分析器产生。  
8 E_NOTICE (int) 运行时通知。表示脚本遇到可能会表现为错误的情况,但是在可以正常运行的脚本里面也可能会有类似的通知。  
16 E_CORE_ERROR (int) 在 PHP 初始化启动过程中发生的致命错误。该错误类似 E_ERROR,但是是由 PHP 引擎核心产生的。  
32 E_CORE_WARNING (int) PHP 初始化启动过程中发生的警告 (非致命错误) 。类似 E_WARNING,但是是由 PHP 引擎核心产生的。  
64 E_COMPILE_ERROR (int) 致命编译时错误。类似 E_ERROR,但是是由 Zend 脚本引擎产生的。  
128 E_COMPILE_WARNING (int) 编译时警告 (非致命错误)。类似 E_WARNING,但是是由 Zend 脚本引擎产生的。  
256 E_USER_ERROR (int) 用户产生的错误信息。类似 E_ERROR,但是是由用户自己在代码中使用 PHP 函数 trigger_error()来产生的。  
512 E_USER_WARNING (int) 用户产生的警告信息。类似 E_WARNING,但是是由用户自己在代码中使用 PHP 函数 trigger_error()来产生的。  
1024 E_USER_NOTICE (int) 用户产生的通知信息。类似 E_NOTICE,但是是由用户自己在代码中使用 PHP 函数 trigger_error()来产生的。  
2048 E_STRICT (int) 启用 PHP 对代码的修改建议,以确保代码具有最佳的互操作性和向前兼容性。  
4096 E_RECOVERABLE_ERROR (int) 可被捕捉的致命错误。 它表示发生了一个可能非常危险的错误,但是还没有导致PHP引擎处于不稳定的状态。 如果该错误没有被用户自定义句柄捕获 (参见 set_error_handler()),将成为一个 E_ERROR 从而脚本会终止运行。  
8192 E_DEPRECATED (int) 运行时通知。启用后将会对在未来版本中可能无法正常工作的代码给出警告。  
16384 E_USER_DEPRECATED (int) 用户产生的警告信息。 类似 E_DEPRECATED, 但是是由用户自己在代码中使用PHP函数 trigger_error()来产生的。  
32767 E_ALL (int) 所有错误、警告和通知。  

上面的值(数值或者符号)用于建立一个二进制位掩码,来制定要报告的错误信息。可以使用按位运算符来组合这些值或者屏蔽某些类型的错误。请注意,在 php.ini 之中,只有'|', '~', '!', '^' 和 '&' 会正确解析。

add a note add a note

User Contributed Notes 21 notes

up
29
Andy at Azurite (co uk)
13 years ago
-1 is also semantically meaningless as a bit field, and only works in 2s-complement numeric representations.  On a 1s-complement system -1 would not set E_ERROR.  On a sign-magnitude system -1 would set nothing at all! (see e.g. http://en.wikipedia.org/wiki/Ones%27_complement)

If you want to set all bits, ~0 is the correct way to do it.

But setting undefined bits could result in undefined behaviour and that means *absolutely anything* could happen :-)
up
26
russthom at fivegulf dot com
12 years ago
[Editor's note: fixed E_COMPILE_* cases that incorrectly returned E_CORE_* strings. Thanks josiebgoode.]

The following code expands on Vlad's code to show all the flags that are set.  if not set, a blank line shows.

<?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
"";
}
?>
up
13
fadhilinjagi at gmail dot com
3 years ago
A simple and neat way to get the error level from the error code. You can even customize the error level names further.

<?php
$exceptions
= [
       
E_ERROR => "E_ERROR",
       
E_WARNING => "E_WARNING",
       
E_PARSE => "E_PARSE",
       
E_NOTICE => "E_NOTICE",
       
E_CORE_ERROR => "E_CORE_ERROR",
       
E_CORE_WARNING => "E_CORE_WARNING",
       
E_COMPILE_ERROR => "E_COMPILE_ERROR",
       
E_COMPILE_WARNING => "E_COMPILE_WARNING",
       
E_USER_ERROR => "E_USER_ERROR",
       
E_USER_WARNING => "E_USER_WARNING",
       
E_USER_NOTICE => "E_USER_NOTICE",
       
E_STRICT => "E_STRICT",
       
E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR",
       
E_DEPRECATED => "E_DEPRECATED",
       
E_USER_DEPRECATED => "E_USER_DEPRECATED",
       
E_ALL => "E_ALL"
];

echo
$exceptions["1"];
$code = 256;
echo
$exceptions[$code];
?>

Output: 
E_ERROR
E_USER_ERROR

This will need updating when PHP updates the error level names. Otherwise, it works just fine.
up
15
bbrokman at gmail dot com
5 years ago
A neat way to have a place in code to control error reporting configuration :)

<?php

$errorsActive
= [
   
E_ERROR             => FALSE,
   
E_WARNING           => TRUE,
   
E_PARSE             => TRUE,
   
E_NOTICE            => TRUE,
   
E_CORE_ERROR        => FALSE,
   
E_CORE_WARNING      => FALSE,
   
E_COMPILE_ERROR     => FALSE,
   
E_COMPILE_WARNING   => FALSE,
   
E_USER_ERROR        => TRUE,
   
E_USER_WARNING      => TRUE,
   
E_USER_NOTICE       => TRUE,
   
E_STRICT            => FALSE,
   
E_RECOVERABLE_ERROR => TRUE,
   
E_DEPRECATED        => FALSE,
   
E_USER_DEPRECATED   => TRUE,
   
E_ALL               => FALSE,
];

error_reporting(
   
array_sum(
       
array_keys($errorsActive, $search = true)
    )
);

?>
up
12
cl at viazenetti dot de
7 years ago
An other way to get all PHP errors  that are set to be reported. This code will even work, when additional error types are added in future.

<?php
$pot
= 0;
foreach (
array_reverse(str_split(decbin(error_reporting()))) as $bit) {
    if (
$bit == 1) {
        echo
array_search(pow(2, $pot), get_defined_constants(true)['Core']). "<br>\n";
    }
   
$pot++;
}
?>
up
11
paulsnar
6 years ago
PHP 7 makes E_STRICT irrelevant, reclassifying most of the errors as proper warnings, notices or E_DEPRECATED: https://wiki.php.net/rfc/reclassify_e_strict
up
3
kezzyhko at NOSPAM dot semysha dot ru
8 years ago
As for me, the best way to get error name by int value is that. And it's works fine for me ;)
<?php

array_flip
(array_slice(get_defined_constants(true)['Core'], 1, 15, true))[$type];

//the same in readable form
array_flip(
   
array_slice(
       
get_defined_constants(true)['Core'],
       
1,
       
15,
       
true
   
)
)[
$type]

?>
up
2
ahsankhatri1992 at gmail
8 years ago
Notes posted above limited to current errors level as on 26th Aug 2016, following snippet will work even on introduction of new error level

$errLvl = error_reporting();
for ( $i = 1; $i < E_ALL;  $i*=2 )
{
    print FriendlyErrorType($errLvl & $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 "";
}
up
1
kaioker
3 years ago
super simple error code to human readable conversion:

function prettycode($code){
    return $code == 0 ? "FATAL" : array_search($code, get_defined_constants(true)['Core']);
}
up
1
ErikBachmann
5 years ago
A shorter version of vladvarna's FriendlyErrorType($type)
<?php
function getErrorTypeByValue($type) {
   
$constants  = get_defined_constants(true);

    foreach (
$constants['Core'] as $key => $value ) { // Each Core constant
       
if ( preg_match('/^E_/', $key  ) ) {    // Check error constants
           
if ( $type == $value )
                return(
"$key=$value");
        }
    }
}  
// getErrorTypeByValue()

echo "[".getErrorTypeByValue( 1 ) . "]". PHP_EOL;
echo
"[".getErrorTypeByValue( 0 ) . "]". PHP_EOL;
echo
"[".getErrorTypeByValue( 8 ) . "]". PHP_EOL;
?>

Will give
    [E_ERROR=1]
    []
    [E_NOTICE=8]
up
1
ali6236 at yahoo dot com
1 year ago
A better way to map error constant to their names (instead of switch) :

    function getErrorName($code)
    {
        static $error_names = [
            E_ERROR => 'E_ERROR',
            E_WARNING => 'E_WARNING',
            E_PARSE => 'E_PARSE',
            E_NOTICE => 'E_NOTICE',
            E_CORE_ERROR => 'E_CORE_ERROR',
            E_CORE_WARNING => 'E_CORE_WARNING',
            E_COMPILE_ERROR => 'E_COMPILE_ERROR',
            E_COMPILE_WARNING => 'E_COMPILE_WARNING',
            E_USER_ERROR => 'E_USER_ERROR',
            E_USER_WARNING => 'E_USER_WARNING',
            E_USER_NOTICE => 'E_USER_NOTICE',
            E_STRICT => 'E_STRICT',
            E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
            E_DEPRECATED => 'E_DEPRECATED',
            E_USER_DEPRECATED => 'E_USER_DEPRECATED',
        ];
        return $error_names[$code] ?? '';
    }
up
0
Anonymous
8 years ago
My version!
For long list function returns for example "E_ALL without E_DEPRECATED "

function errorLevel()
{
    $levels = array(
        '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_STRICT',
        'E_RECOVERABLE_ERROR',
        'E_DEPRECATED',
        'E_USER_DEPRECATED',
        'E_ALL'
    );
    $excluded = $included = array();
    $errLvl = error_reporting();
    foreach ($levels as $lvl) {
        $val = constant($lvl);
        if ($errLvl & $val) {
            $included []= $lvl;
        } else {
                $excluded []= $lvl;
        }
    }
    if (count($excluded) > count($included)) {
        echo '<br />Consist: '.implode(',', $included);
    } else {
        echo '<br />Consist: E_ALL without '.implode(',', $excluded);
    }
}
up
-2
PhpMyCoder
14 years ago
Well, technically -1 will show all errors which includes any new ones included by PHP. My guess is that E_ALL will always include new error constants so I usually prefer:

<?php
error_reporting
(E_ALL | E_STRICT);
?>

Reason being: With a quick glance anyone can tell you what errors are reported. -1 might be a bit more cryptic to newer programmers.
up
-3
vladvarna at gmail dot com
12 years ago
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_CORE_ERROR: // 64 //
                return 'E_COMPILE_ERROR';
            case E_CORE_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 $type;
    }
up
-5
Anonymous
12 years ago
this would give you all the reported exception list of your configuration.

<?php
function FriendlyErrorType($type)
{
   
$return ="";
    if(
$type & E_ERROR) // 1 //
       
$return.='& E_ERROR ';
    if(
$type & E_WARNING) // 2 //
       
$return.='& E_WARNING ';
    if(
$type & E_PARSE) // 4 //
       
$return.='& E_PARSE ';
    if(
$type & E_NOTICE) // 8 //
       
$return.='& E_NOTICE ';
    if(
$type & E_CORE_ERROR) // 16 //
       
$return.='& E_CORE_ERROR ';
    if(
$type & E_CORE_WARNING) // 32 //
       
$return.='& E_CORE_WARNING ';
    if(
$type & E_COMPILE_ERROR) // 64 //
       
$return.='& E_COMPILE_ERROR ';
    if(
$type & E_COMPILE_WARNING) // 128 //
       
$return.='& E_COMPILE_WARNING ';
    if(
$type & E_USER_ERROR) // 256 //
       
$return.='& E_USER_ERROR ';
    if(
$type & E_USER_WARNING) // 512 //
       
$return.='& E_USER_WARNING ';
    if(
$type & E_USER_NOTICE) // 1024 //
       
$return.='& E_USER_NOTICE ';
    if(
$type & E_STRICT) // 2048 //
       
$return.='& E_STRICT ';
    if(
$type & E_RECOVERABLE_ERROR) // 4096 //
       
$return.='& E_RECOVERABLE_ERROR ';
    if(
$type & E_DEPRECATED) // 8192 //
       
$return.='& E_DEPRECATED ';
    if(
$type & E_USER_DEPRECATED) // 16384 //
       
$return.='& E_USER_DEPRECATED ';
    return
substr($return,2);
}
echo
"error_reporting = " . FriendlyErrorType(ini_get('error_reporting')) .";<br>";
?>
up
-7
Herbert
8 years ago
if you want to bring this list back to the categories error/warning/notice/all

<?php

$error_level  
= 'warning';      //  Allowed values: error/warning/notice/all

$error_error   = (int)                  E_ERROR | E_USER_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_PARSE;
$error_warning = (int) $error_error   | E_WARNING | E_USER_WARNING | E_CORE_WARNING | E_COMPILE_WARNING;
$error_notice  = (int) $error_warning | E_NOTICE | E_USER_NOTICE | E_DEPRECATED | E_USER_DEPRECATED;
$error_all     = (int) $error_notice  | E_STRICT;

error_reporting ($GLOBALS["error_$error_level"]);

?>
up
-5
damian at thebestisp dot dot dot com
8 years ago
I use this code to help mimic the default error handler, the only difference is that the levels end up being all caps, which I don't care to fix. You could also get rid of the underscores, but again, I don't care :P
Until php starts adding constants starting with E_ that have values overlapping with other E_ constants, this seems to be the shortest way of converting error code integers to strings understandable by meat bags. It will also work with new types, so that's nice.
<?php
function friendly_error_type($type) {
    static
$levels=null;
    if (
$levels===null) {
       
$levels=[];
        foreach (
get_defined_constants() as $key=>$value) {
            if (
strpos($key,'E_')!==0) {continue;}
           
$levels[$value]=substr($key,2);
        }
    }
    return (isset(
$levels[$type]) ? $levels[$type] : "Error #{$type}");
}
echo
friendly_error_type(1); #ERROR
echo friendly_error_type(2); #WARNING
echo friendly_error_type(3); #Error #3
?>
Tested on 5.6.12 and 7.0.3 (The first was by accident, didn't realize I was sshed into production :3)
up
-6
damian at thebestisp dot dot dot com
8 years ago
I saw that Chris seems to think that errors might be combined in some cases, I don't know of any cases, but his code is overly verbose, inefficient, and doesn't take into account future E_ constants. Here's my version of handling multiple errors (which probably wont ever happen) using my other code as a base. The only real difference is that this doesn't bother to split out undefined bits, which is pretty much useless and would get rather messy if you have more than a few bits set above 2**14 (0 to 14 have an associated error).

<?php
function friendly_error_type($type) {
    static
$levels=null;
    if (
$levels===null) {
       
$levels=[];
        foreach (
get_defined_constants() as $key=>$value) {
            if (
strpos($key,'E_')!==0) {continue;}
           
$levels[$value]=substr($key,2);
        }
    }
   
$out=[];
    foreach (
$levels as $int=>$string) {
        if (
$int&$type) {$out[]=$string;}
       
$type&=~$int;
    }
    if (
$type) {$out[]="Error Remainder [{$type}]";}
    return
implode(' & ',$out);
}
echo
friendly_error_type(E_ERROR|E_USER_DEPRECATED); //ERROR & USER_DEPRECATED
echo friendly_error_type(2**20-1); //ERROR & RECOVERABLE_ERROR & WARNING & PARSE & NOTICE & STRICT & DEPRECATED & CORE_ERROR & CORE_WARNING & COMPILE_ERROR & COMPILE_WARNING & USER_ERROR & USER_WARNING & USER_NOTICE & USER_DEPRECATED & Error Remainder [1015808]
?>
up
-4
ilpaijin at gmail dot com
7 years ago
The bitmask values relative to the Constant E_ALL is 30719, in case of PHP 5.6.x
up
-6
espertalhao04 at hotmail dot com
7 years ago
A simplified way of writting a function to return a (non-optimal) representation of the error code as a bitwise string:

<?php
   
function readable_error_type($error_code)
    {
       
$constants = array();
        foreach(
get_defined_constants() as $key => $value)
        {
            if(
strpos($key, 'E_') === 0 && ($value <= $error_code) && ($value & $error_code))
            {
               
$constants[] = $key;
            }
        }

        return
implode(' | ', $constants);
    }
?>

This only works for values above 0.
up
-18
chris-php at IGNORETHISPART dot cybermato dot com
10 years ago
How about this?  Unlike the examples below, it will show all the bits that are set, AND handle any bits defined in the future (at least not silently hide them)...

    $strErrorType = "";
    $bit = 1;
    $tmpErrNo = $errNo;
    while ($tmpErrNo) {
        if ($tmpErrNo & $bit) {
            if ($strErrorType != "")
                $strErrorType .= " | ";
            switch ($bit) {
            case E_USER_WARNING:
                $strErrorType .= "E_USER_WARNING"; break;
            case E_USER_NOTICE:
                $strErrorType .= "E_USER_NOTICE"; break;
            case E_WARNING:
                $strErrorType .= "E_WARNING"; break;
            case E_CORE_WARNING:
                $strErrorType .= "E_CORE_WARNING"; break;
            case E_COMPILE_WARNING:
                $strErrorType .= "E_COMPILE_WARNING"; break;
            case E_NOTICE:
                $strErrorType .= "E_NOTICE"; break;
            case E_ERROR:
                $strErrorType .= "E_ERROR"; break;
            case E_PARSE:
                $strErrorType .= "E_PARSE"; break;
            case E_CORE_ERROR:
                $strErrorType .= "E_CORE_ERROR"; break;
            case E_COMPILE_ERROR:
                $strErrorType .= "E_COMPILE_ERROR"; break;
            case E_USER_ERROR:
                $strErrorType .= "E_USER_ERROR"; break;  
            default:
                $strErrorType .= "(unknown error bit $bit)"; break;
            }
        }
        $tmpErrNo &= ~$bit;
        $bit <<= 1;
    }
To Top