1

Im executing the list of php codes via eval, If there is any error in code i want to display this code have fatal error/parse error.

is there any way to give custom message for fatal error or any other error

My code is like this :

$output = [];
foreach($codes as $key => $res) {

    if(eval($res['code'])) {
        eval($res['code']);
        $output[$key] = $result;
    } else {
        $output[$key]  = "Fatal error in code";             
    }       
}
var_dump($output);
sk2
  • 1,101
  • 1
  • 10
  • 24
  • maybe useful? [Handle fatal errors in PHP using register_shutdown_function()](http://stackoverflow.com/questions/4410632/handle-fatal-errors-in-php-using-register-shutdown-function). Actually, quite useful. It is really useful for debugging 'awkward' issues that are triggering fatal errors. – Ryan Vincent Aug 16 '15 at 19:57

2 Answers2

1

There is a way! Write the code to a file, using php -l in the eval, then delete the file. See my answer for example code:

Is there any way to catch fatal error using eval()?

Community
  • 1
  • 1
Frank Forte
  • 1,797
  • 15
  • 18
0

The trick is to expect Throwable:

try {
    ...
    @eval(...);
    ...
} catch(Throwable $t) {
    ...
}
Rick James
  • 122,779
  • 10
  • 116
  • 195