Skip to content

Instantly share code, notes, and snippets.

@ginkgomzd
Last active June 16, 2017 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ginkgomzd/d3b757ff543d6135141dce50aa430c77 to your computer and use it in GitHub Desktop.
Save ginkgomzd/d3b757ff543d6135141dce50aa430c77 to your computer and use it in GitHub Desktop.
Test or List PHP Error Reporting Flags
<?php
// example call, specifying bit-flags,
// returns as string:
echo listEnabledErrorReportingFlags(E_ALL & ~(E_WARNING|E_NOTICE|E_STRICT));
// example call, using current bit-flags,
// returns as array:
var_export(listEnabledErrorReportingFlags(null, TRUE));
/**
* List the error_reporting levels enabled.
* Calls error_reporting() if bitflags are not provided.
*
* @param int $error_reporting bitflags used for php error_reporting
* @param boolean $asArray result as array, indexed by bitflag integers
* @return String string or array
*/
function listEnabledErrorReportingFlags($error_reporting=NULL,$asArray=FALSE) {
if (!isset($error_reporting)) {
$error_reporting = error_reporting();
}
$list = ($asArray)? array() : '' ;
foreach (getErrorReportingLevelMap() as $level => $label) {
$index = ($asArray) ? $level : FALSE;
if ($error_reporting == E_ALL) {
addToList($list, $errorLevels[E_ALL], $index);
break;
}
if ($level == E_ALL) {
continue;
}
if ($error_reporting & $level) {
addToList($list, $label, $index);
}
}
return $list;
}
/**
* If index is provied, assumes $list is an array.
* Else assumes list is a string.
* @param [type] $list String or Array to append to.
* @param String $label value added
* @param boolean $index Optional
*/
function addToList(&$list, $label, $index=FALSE) {
if ($index) {
$list[$index] = $label;
} else {
$list .= $label."\n";
}
}
function getErrorReportingLevelMap() {
return array(
E_ERROR =>'E_ERROR: Fatal run-time errors',
E_WARNING =>'E_WARNING: Run-time warnings (non-fatal errors).',
E_PARSE =>'E_PARSE: Compile-time parse errors.',
E_NOTICE =>'E_NOTICE: Run-time notices.',
E_CORE_ERROR =>'E_CORE_ERROR: This is like an E_ERROR, except it is generated by the core of PHP.',
E_CORE_WARNING =>'E_CORE_WARNING: This is like an E_WARNING, except it is generated by the core of PHP.',
E_COMPILE_ERROR =>'E_COMPILE_ERROR: Fatal compile-time errors.',
E_COMPILE_WARNING =>'E_COMPILE_WARNING: Compile-time warnings (non-fatal errors).',
E_USER_ERROR =>'E_USER_ERROR: User-generated error message.',
E_USER_WARNING =>'E_USER_WARNING: User-generated warning message.',
E_USER_NOTICE =>'E_USER_NOTICE: User-generated notice message.',
E_STRICT => 'E_STRICT:',
E_RECOVERABLE_ERROR =>'E_RECOVERABLE_ERROR: Catchable fatal error.',
E_DEPRECATED =>'E_DEPRECATED: Run-time notices.',
E_USER_DEPRECATED =>'E_USER_DEPRECATED: User-generated warning message.',
E_ALL =>'E_ALL:',
);
}
// https://secure.php.net/manual/en/errorfunc.constants.php
// Errors and Logging 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment