预定义常量
下列常量作为 PHP 核心的一部分总是可用的。
以下常量(相应的数值或其符号名称)用作位掩码来指定要报告的错误。可以使用按位运算符来组合这些值或屏蔽某些类型的错误。
小技巧
常量的名称可以在 php.ini 中使用,而不是它们对应的原始数值。但是,php.ini 中只能理解
|
、~
、^
、!
、&
运算符。
警告
无法在 PHP 之外使用符号名称。例如,在 httpd.conf 中,必须使用计算出的位掩码值。
-
E_ERROR
(int) -
致命的运行时错误。这类错误一般是不可恢复的情况,例如内存分配导致的问题。后果是导致脚本终止不再继续运行。
常量值
1
-
E_WARNING
(int) -
运行时警告 (非致命错误)。仅给出提示信息,但是脚本不会终止运行。
常量值:
2
-
E_PARSE
(int) -
编译时解析错误。解析错误只由解析器产生。
常量值:
4
-
E_NOTICE
(int) -
运行时通知。表示脚本遇到可能会表现为错误的情况,但是在可以正常运行的脚本里面也可能会有类似的通知。
常量值:
8
-
E_CORE_ERROR
(int) -
在 PHP 初始化启动过程中发生的致命错误。该错误类似
E_ERROR
,但是是由 PHP 引擎核心产生。 常量值:16
-
E_CORE_WARNING
(int) -
PHP 初始化启动过程中发生的警告 (非致命错误) 。类似
E_WARNING
,但是是由 PHP 引擎核心产生。 常量值:32
-
E_COMPILE_ERROR
(int) -
致命编译时错误。类似
E_ERROR
,但是是由 Zend 脚本引擎产生。 常量值:64
-
E_COMPILE_WARNING
(int) -
编译时警告 (非致命错误)。类似
E_WARNING
,但是是由 Zend 脚本引擎产生。 常量值:128
-
E_DEPRECATED
(int) -
运行时弃用通知。启用后将会对在未来版本中可能无法正常工作的代码给出警告。
常量值:
8192
-
E_USER_ERROR
(int) -
用户产生的错误信息。类似
E_ERROR
,但是是由用户自己在代码中使用 PHP 函数 trigger_error() 来产生。 常量值:256
警告自 PHP 8.4.0 起,已弃用此常量与 trigger_error() 一起使用的用法。建议改为
throw
Exception 或调用 exit()。 -
E_USER_WARNING
(int) -
用户产生的警告信息。类似
E_WARNING
,由用户自己在代码中使用 PHP 函数 trigger_error() 来产生。 常量值:512
-
E_USER_NOTICE
(int) -
用户产生的通知信息。类似
E_NOTICE
,由用户自己在代码中使用 PHP 函数 trigger_error() 来产生。 常量值:1024
-
E_USER_DEPRECATED
(int) -
用户产生的警告信息。 类似
E_DEPRECATED
, 由用户自己在代码中使用 PHP 函数 trigger_error() 来产生。 常量值:16384
-
E_STRICT
(int) -
PHP 发出有关执行代码的运行时建议,以确保向前兼容。
常量值:
2048
警告此错误级别未使用,且自 PHP 8.4.0 起已弃用。
-
E_RECOVERABLE_ERROR
(int) -
旧引擎“exception”对应于可捕获的致命错误。与 Error
类似,但必须通过用户定义的错误处理程序捕获(请参阅
set_error_handler())。如果不处理,则其行为类似于
E_ERROR
。 常量值:4096
注意: 此错误级别实际上未使用,唯一可能发生这种情况的情况是将 object 解释为 bool 失败。这只会发生在内部对象中。 PHP 8.4.0 之前,最常见的示例是在条件中使用 GMP 实例。
-
E_ALL
(int) -
包含每个错误、警告和通知的位掩码。
常量值:
32767
+添加备注
用户贡献的备注 8 notes
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 :-)
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++;
}
?>
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 "";
}
?>
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)
)
);
?>
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.
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]
?>
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']);
}
ErikBachmann ¶
5 years ago
<?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]