From: maggus dot staab at googlemail dot com
Operating system:
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Bug
Bug description:Inefficent example code
Description:
------------
the example at http://php.net/manual/en/function.set-error-handler.php
uses
[code]
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
return;
}
// ...
[/code]
which filters error within the error handler, instead of registering the
error handler only for certain E_* levels.
A way more effienct way would be
[code]
$old_error_handler = set_error_handler("myErrorHandler",
error_reporting());
[/code]
which would prevent a lot of unnecessary invocations of the
error-handler which would be a good thing (perf-wise)
Expected result:
----------------
example code should be
[code]
<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br
/>\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// function to test the error handling
function scale_by_log($vect, $scale)
{
if (!is_numeric($scale) || $scale <= 0) {
trigger_error("log(x) for x <= 0 is undefined, you used: scale =
$scale", E_USER_ERROR);
}
if (!is_array($vect)) {
trigger_error("Incorrect input vector, array of values
expected", E_USER_WARNING);
return null;
}
$temp = array();
foreach($vect as $pos => $value) {
if (!is_numeric($value)) {
trigger_error("Value at position $pos is not a number, using
0 (zero)", E_USER_NOTICE);
$value = 0;
}
$temp[$pos] = log($scale) * $value;
}
return $temp;
}
// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler",
error_reporting());
// trigger some errors, first define a mixed array with a non-numeric
item
echo "vector a\n";
$a = array(2, 3, "foo", 5.5, 43.3, 21.11);
print_r($a);
// now generate second array
echo "----\nvector b - a notice (b = log(PI) * a)\n";
/* Value at position $pos is not a number, using 0 (zero) */
$b = scale_by_log($a, M_PI);
print_r($b);
// this is trouble, we pass a string instead of an array
echo "----\nvector c - a warning\n";
/* Incorrect input vector, array of values expected */
$c = scale_by_log("not array", 2.3);
var_dump($c); // NULL
// this is a critical error, log of zero or negative number is
undefined
echo "----\nvector d - fatal error\n";
/* log(x) for x <= 0 is undefined, you used: scale = $scale" */
$d = scale_by_log($a, -2.5);
var_dump($d); // Never reached
?>
[code]
--
Edit bug report at https://bugs.php.net/bug.php?id=68982&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68982&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68982&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68982&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68982&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68982&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68982&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68982&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68982&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68982&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68982&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68982&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68982&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68982&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68982&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68982&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68982&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68982&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68982&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68982&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68982&r=mysqlcfg