note 65824 deleted from function.error-reporting by bjori
| From: | bjori@php.net | Date: | Wed, 17 May 2006 13:32:00 +0000 |
| Subject: | note 65824 deleted from function.error-reporting by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-111498@lists.php.net to get a copy of this message | ||
Note Submitter: Chris
----
@ the_bug_the_bug at hotmail dot com
I'd extract the E_ALL first, because using it can shorter the string.
<?php
function error2string($value)
{
$level_names = array(
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_ALL => 'E_ALL' );
if(defined(E_STRICT)) $errorlevels[E_STRICT]='E_STRICT';
$levels=array();
if($value&E_ALL)
{
$levels[]='E_ALL';
$value&=~E_ALL;
}
foreach($level_names as $level=>$name)
if($value&$level) $levels[]=$name;
return implode(' | ',$levels);
}
?>
And a little function for opposite conversion.
<?php
function string2error($string)
{
$levels=explode('|',$string);
foreach($levels as $level)
{
$level=trim($level);
if(defined($level)) $value|=(int)constant($level);
}
return $value;
}
?>
If you want to make sure string2error uses only E_* constants, then just replace defined($level)
with in_array($level,$level_names) (using the array from the example above). And if you plan to use
both functions, you may as well define $level_names as a global var for better performance ;)