|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #68982 Inefficent example code
Submitted: 2015-02-04 15:03 UTC Modified: 2017-01-28 12:52 UTC
From: maggus dot staab at googlemail dot com Assigned:
Status: Open Package: Unknown/Other Function
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-02-04 15:03 UTC] maggus dot staab at googlemail dot com
the example at uses

function myErrorHandler($errno, $errstr, $errfile, $errline)
    if (!(error_reporting() & $errno)) {
// ...

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 
$old_error_handler = set_error_handler("myErrorHandler", error_reporting());

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

// 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";

    case E_USER_WARNING:
        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";

    case E_USER_NOTICE:
        echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";

        echo "Unknown error type: [$errno] $errstr<br />\n";

    /* 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);

// 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);

// 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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-01-28 12:52 UTC]
-Type: Bug +Type: Documentation Problem -Package: Documentation problem +Package: Unknown/Other Function
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jul 12 22:01:30 2024 UTC