Predefined Constants
The constants below are always available as part of the PHP core.
Note: You may use these constant names in php.ini but not outside of PHP, like in httpd.conf, where you'd use the bitmask values instead.
Value | Constant | Description | Note |
---|---|---|---|
1 |
E_ERROR
(integer)
|
Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted. | |
2 |
E_WARNING
(integer)
|
Run-time warnings (non-fatal errors). Execution of the script is not halted. | |
4 |
E_PARSE
(integer)
|
Compile-time parse errors. Parse errors should only be generated by the parser. | |
8 |
E_NOTICE
(integer)
|
Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script. | |
16 |
E_CORE_ERROR
(integer)
|
Fatal errors that occur during PHP's initial startup. This is like an
E_ERROR , except it is generated by the core of PHP.
|
|
32 |
E_CORE_WARNING
(integer)
|
Warnings (non-fatal errors) that occur during PHP's initial startup.
This is like an E_WARNING , except it is generated
by the core of PHP.
|
|
64 |
E_COMPILE_ERROR
(integer)
|
Fatal compile-time errors. This is like an E_ERROR ,
except it is generated by the Zend Scripting Engine.
|
|
128 |
E_COMPILE_WARNING
(integer)
|
Compile-time warnings (non-fatal errors). This is like an
E_WARNING , except it is generated by the Zend
Scripting Engine.
|
|
256 |
E_USER_ERROR
(integer)
|
User-generated error message. This is like an
E_ERROR , except it is generated in PHP code by
using the PHP function trigger_error().
|
|
512 |
E_USER_WARNING
(integer)
|
User-generated warning message. This is like an
E_WARNING , except it is generated in PHP code by
using the PHP function trigger_error().
|
|
1024 |
E_USER_NOTICE
(integer)
|
User-generated notice message. This is like an
E_NOTICE , except it is generated in PHP code by
using the PHP function trigger_error().
|
|
2048 |
E_STRICT
(integer)
|
Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. | Since PHP 5 but not included in E_ALL until PHP 5.4.0 |
4096 |
E_RECOVERABLE_ERROR
(integer)
|
Catchable fatal error. It indicates that a probably dangerous error
occurred, but did not leave the Engine in an unstable state. If the error
is not caught by a user defined handle (see also
set_error_handler()), the application aborts as it
was an E_ERROR .
|
Since PHP 5.2.0 |
8192 |
E_DEPRECATED
(integer)
|
Run-time notices. Enable this to receive warnings about code that will not work in future versions. | Since PHP 5.3.0 |
16384 |
E_USER_DEPRECATED
(integer)
|
User-generated warning message. This is like an
E_DEPRECATED , except it is generated in PHP code by
using the PHP function trigger_error().
|
Since PHP 5.3.0 |
32767 |
E_ALL
(integer)
|
All errors and warnings, as supported, except of level
E_STRICT prior to PHP 5.4.0.
|
32767 in PHP 5.4.x, 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously |
The above values (either numerical or symbolic) are used to build up a bitmask that specifies which errors to report. You can use the bitwise operators to combine these values or mask out certain types of errors. Note that only '|', '~', '!', '^' and '&' will be understood within php.ini.
Коментарии
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.
-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 :-)
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;
}
[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 "";
}
?>
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)
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);
}
}
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]
?>
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 "";
}
The bitmask values relative to the Constant E_ALL is 30719, in case of PHP 5.6.x
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++;
}
?>
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
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)
)
);
?>
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]
super simple error code to human readable conversion:
function prettycode($code){
return $code == 0 ? "FATAL" : array_search($code, get_defined_constants(true)['Core']);
}
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.
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] ?? '';
}