Forum Moderators: coopster
eval("print 3+2");
Results in
Parse error: parse error in /home/www/test.php(2) : eval()'d code on line 1
I would like to capture this and report the error in a nice way - then continue processing other things.
Reading so far I don't think there is a way to do this... I tried output buffering but it is a fatal error and causes the script to halt.
Does anyone either know a better way? Anyway to pre-test the statement? Or can confirm my hunch that it's not possible (yet).
I found this somewhere once, can't remember where I first seen the link though...
h**p://www.mojavelinux.com/cooker/demos/errorReporter/
Maybe you'll get some helpful ideas reviewing the code.
There are some really nice error handling features being added. Liked the errorReporter as well... I'll go through it in more detail soon.
Turns out I was being dumb. My initial attempt at output buffering was flawed, but managed to get it working in a much more simple scenario.
//$eval = '$myeval = 3+2; print $myeval;';
$eval = '$myeval = 3+2; print $myeval';ob_start();
eval($eval);
$ret = ob_get_contents();
ob_end_clean();
print "<P>Not halted :)</P>";
if (preg_match("/Parse error/",$ret)) {
print "<P>A Parse error occured</P>";
// do something smart
} else {
print "<P>The evaled code was fine.</P>";
}
print "<P>Result: $ret</P>";
print "<P>Value: $myeval</P>";
Demonstrates that it works fine.
The output buffering does not capture fatal errors, so thats where I need to use a custom error handler - which I haven't written yet.
Example: $eval = '$myeval = 3+2; undefined_func($myeval);';
Causes:
Fatal error: Call to undefined function: undefined_func() in /home/www/html/eval.php(7) : eval()'d code on line 1
I will continue to update the thread as I make progress but right now - I have to work on something else ;)