Jump to content

[SOLVED] Error Handling- foreach problem


HoTDaWg

Recommended Posts

hi,

 

im creating a script which can echo multiple errors, but im having a lot of trouble with the foreach statement.i read the manual and everthing but for once, forgive me for saying this but, it didnt help :(

 

heres my code:

<?php
error_reporting(E_ALL);
function ErrorHandler($errno,$errmsg,$file,$lineno){
   $errortype = array (
               E_ERROR              => 'Error',
               E_WARNING            => 'Warning',
               E_PARSE              => 'Parsing Error',
               E_NOTICE            => 'Notice',
               E_CORE_ERROR        => 'Core Error',
               E_CORE_WARNING      => 'Core Warning',
               E_COMPILE_ERROR      => 'Compile Error',
               E_COMPILE_WARNING    => 'Compile Warning',
               E_USER_ERROR        => 'User Error',
               E_USER_WARNING      => 'User Warning',
               E_USER_NOTICE        => 'User Notice',
               );
foreach ($errno as &$errortype){
echo"ERRORFOUND!";
}
}
?>

thanks.

 

HoTDaWg

Link to comment
Share on other sites

sorry, i get the following error:


Warning: Invalid argument supplied for foreach() in /home/idanc48d/public_html/v2/errorhandling.php on line 17

Warning: Invalid argument supplied for foreach() in /home/idanc48d/public_html/v2/errorhandling.php on line 17

Warning: Invalid argument supplied for foreach() in /home/idanc48d/public_html/v2/errorhandling.php on line 17

 

Link to comment
Share on other sites

hmm, i dont understand. i changed some things around but it still doesnt work:S

<?php
error_reporting(E_ALL);
function ErrorHandler($errno,$errmsg,$file,$lineno){
$errorno=array('E_USER_WARNING','E_USER_NOTICE','E_NOTICE','E_COMPILE_WARNING','E_CORE_WARNING','E_WARNING','E_USER_ERROR','E_COMPILE_ERROR','E_CORE_ERROR','E_ERROR','E_PARSE');
   $errortype = array (
               E_ERROR              => 'Error',
               E_WARNING            => 'Warning',
               E_PARSE              => 'Parsing Error',
               E_NOTICE            => 'Notice',
               E_CORE_ERROR        => 'Core Error',
               E_CORE_WARNING      => 'Core Warning',
               E_COMPILE_ERROR      => 'Compile Error',
               E_COMPILE_WARNING    => 'Compile Warning',
               E_USER_ERROR        => 'User Error',
               E_USER_WARNING      => 'User Warning',
               E_USER_NOTICE        => 'User Notice',
               );
foreach ($errno as &$errortype){
echo"ERRORFOUND!";
}
}
?>

it gives me the following error:


Warning: Invalid argument supplied for foreach() in /home/idanc48d/public_html/v2/errorhandling.php on line 18

Warning: Invalid argument supplied for foreach() in /home/idanc48d/public_html/v2/errorhandling.php on line 18

Warning: Invalid argument supplied for foreach() in /home/idanc48d/public_html/v2/errorhandling.php on line 18

Link to comment
Share on other sites

Okay, but you still didn't show me where you call it. You need to pass it an ARRAY for $errno, because whatever your passing it now as the first argument is NOT an array.

 

Try:

if(count($errno) > 0){
   foreach ($errno as &$errortype){
      echo"ERRORFOUND!";
   }
}

 

PS: Using a reference value in foreach only works in PHP5, IIRC.

Link to comment
Share on other sites

see, according to the manual

"errno

    The first parameter, errno, contains the level of the error raised, as an integer. "

 

So $errno will never be an array, and you'll never be able to use foreach on it.

Perhaps what you're trying to do is over my head, but I don't see how you're ever going to pass it an array as the first argument,

Link to comment
Share on other sites

sorry for my lack of communication. i see where you are going. basically i want to make my script so that for each error something should happen (in this case an echo). i had an orginal script which basically detected an an error and then echoed the problem but it only displayed one error not multiple. here is the script:

<?php
function ErrorHandler($errno,$errmsg,$file,$lineno){
if($errnum==E_USER_WARNING){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_USER_NOTICE){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_NOTICE){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_USER_NOTICE){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_COMPILE_WARNING){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_CORE_WARNING){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_WARNING){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_USER_NOTICE){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_USER_ERROR){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_COMPILE_ERROR){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_COMPILE_NOTICE){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_USER_NOTICE){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_CORE_ERROR){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_ERROR){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
  if($errnum==E_PARSE){
    echo 'Error: '.$errmsg.'<br />File: '.$file.'<br />Line: '.$lineno;
    exit();
  }
}
?>
[code]

[/code]

Link to comment
Share on other sites

Because that function only ever gets ONE number for $errno.

I still don't see how you're going to pass it anything but one integer. If you explain how you can get $errno to be an array instead of one int, then we can go from there. But since PHP handles the errors, I don't see it.

 

Using trigger_error, you can still only pass it an INT for the errno.

 

Show me an example of when you create an error that can have more than one error.

 

Perhaps you want to make an array of all your Errnos, then do foreach and trigger the error in that foreach?

Link to comment
Share on other sites

thats the problem, :( i havent found a script yet that can deal with multiple errors. the one in the manual (http://ca.php.net/errorfunc) is:

<?php
// we will do our own error handling
error_reporting(0);

// user defined error handling function
function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
{
   // timestamp for the error entry
   $dt = date("Y-m-d H:i:s (T)");

   // define an assoc array of error string
   // in reality the only entries we should
   // consider are E_WARNING, E_NOTICE, E_USER_ERROR,
   // E_USER_WARNING and E_USER_NOTICE
   $errortype = array (
               E_ERROR              => 'Error',
               E_WARNING            => 'Warning',
               E_PARSE              => 'Parsing Error',
               E_NOTICE            => 'Notice',
               E_CORE_ERROR        => 'Core Error',
               E_CORE_WARNING      => 'Core Warning',
               E_COMPILE_ERROR      => 'Compile Error',
               E_COMPILE_WARNING    => 'Compile Warning',
               E_USER_ERROR        => 'User Error',
               E_USER_WARNING      => 'User Warning',
               E_USER_NOTICE        => 'User Notice',
               E_STRICT            => 'Runtime Notice',
               E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'
               );
   // set of errors for which a var trace will be saved
   $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
  
   $err = "<errorentry>\n";
   $err .= "\t<datetime>" . $dt . "</datetime>\n";
   $err .= "\t<errornum>" . $errno . "</errornum>\n";
   $err .= "\t<errortype>" . $errortype[$errno] . "</errortype>\n";
   $err .= "\t<errormsg>" . $errmsg . "</errormsg>\n";
   $err .= "\t<scriptname>" . $filename . "</scriptname>\n";
   $err .= "\t<scriptlinenum>" . $linenum . "</scriptlinenum>\n";

   if (in_array($errno, $user_errors)) {
       $err .= "\t<vartrace>" . wddx_serialize_value($vars, "Variables") . "</vartrace>\n";
   }
   $err .= "</errorentry>\n\n";
  
   // for testing
   // echo $err;

   // save to the error log, and e-mail me if there is a critical user error
   error_log($err, 3, "/usr/local/php4/error.log");
   if ($errno == E_USER_ERROR) {
       mail("phpdev@example.com", "Critical User Error", $err);
   }
}


function distance($vect1, $vect2)
{
   if (!is_array($vect1) || !is_array($vect2)) {
       trigger_error("Incorrect parameters, arrays expected", E_USER_ERROR);
       return NULL;
   }

   if (count($vect1) != count($vect2)) {
       trigger_error("Vectors need to be of the same size", E_USER_ERROR);
       return NULL;
   }

   for ($i=0; $i<count($vect1); $i++) {
       $c1 = $vect1[$i]; $c2 = $vect2[$i];
       $d = 0.0;
       if (!is_numeric($c1)) {
           trigger_error("Coordinate $i in vector 1 is not a number, using zero",
                           E_USER_WARNING);
           $c1 = 0.0;
       }
       if (!is_numeric($c2)) {
           trigger_error("Coordinate $i in vector 2 is not a number, using zero",
                           E_USER_WARNING);
           $c2 = 0.0;
       }
       $d += $c2*$c2 - $c1*$c1;
   }
   return sqrt($d);
}

$old_error_handler = set_error_handler("userErrorHandler");

// undefined constant, generates a warning
$t = I_AM_NOT_DEFINED;

// define some "vectors"
$a = array(2, 3, "foo");
$b = array(5.5, 4.3, -1.6);
$c = array(1, -3);

// generate a user error
$t1 = distance($c, $b) . "\n";

// generate another user error
$t2 = distance($b, "i am not an array") . "\n";

// generate a warning
$t3 = distance($a, $b) . "\n";

?> 

 

Link to comment
Share on other sites

So instead of calling trigger_error everytime you want an error, put the error messages and the nums into an array, and then when you want to display all these errors, loop through that one and call trigger_error.

 

Otherwise I don't see how you could do what you're saying.

Link to comment
Share on other sites

See in the example, they use it.

 

You keep saying you want it to print multiple errors, but you don't know how you will pass it those multiple errors. That's not really how error handling works. You have to create each error, or the error gets triggered by the code. As far as I can tell, there is no way to pass multiple errors to the error handler, you have to do each one individually.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.