2

Sometimes my scripts produce an error, mostly a Warning. I have sometimes an idea why this happens, but I have also no clue sometimes why it happens.

Now my question: Is it possible that if a warning gets showed, I can see for what was in the variable?

"Warning: Invalid argument supplied for foreach() in ....";

I get this message but no clue, what was in the variable. The problem is, it's a script running few hours, with different data, so it's hard to reproduce it. Because, I don't know what was in the variable.

I need this for all kind of Warnings / Errors / Notice / Fatal Error etc.

Thanks for the help.

P.S.

4 Answers 4

2

you have a full chapter in php dedicated to errors: http://www.php.net/manual/en/book.errorfunc.php

From the php manual: http://www.php.net/manual/en/function.set-error-handler.php

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting
        return;
    }

    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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Error handler can also take a fifth parameter, $errcontext, which contains the value of $GLOBALS (or function scope) at the time of the error. If your error comes from foreach ($myArray as $value) then $errcontext['myArray'] will contain your troublesome value.
0

There are a lot of warnings/errors, usually you should just apply logic.

Warning: Invalid argument supplied for foreach() in ....

This means that some variable in the foreach is invalid:

foreach ($array as $value)

or

foreach ($array as $key => $value)

If your script does not match that exact syntax, or if $array is not an actual array, an error would be triggered.

1 Comment

i know that, the problem, is the foreach loop works like 10000+ loops than it gets not a array only a var, and i need to find out, what was in the loop and to find out how to fix it :) but thanks for help
0

It's not easy to design a debbuging workaround for long-time usage with logging. Basically in this case you would need to adapt the specific foreach with:

foreach (must_be_array($var) as $whatever) ...

Then define that assertion function:

function must_be_array($var) {
     if (is_array($var)) return $var;

     print_r(array_slice(debug_backtrace(), 1));

     // return array();    // to remove now redundant warning
}

The debug_backtrace gives additional context information (called functions and parameters might be helpful). But it won't tell you what should have been in the array variable. It's still going to be an empty variable.

Comments

0

Take a look at set_error_handler() and debug_backtrace().

Also, if there is a specific line/foreach loop that regularly produces this error, add some code something like this on the line before it...

if (!is_array($shouldBeAnArry)) {
  // log something here, including data about the variable
  // from var_dump(), debug_backtrace() etc etc
} else {
  // do the loop
}

This is true of all errors/warnings etc - if an error pops up, add some validation to counter the cause of the error on the line(s) before.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.