sk2
sk2

Reputation: 1181

Is there any way to catch eval fatal error in php?

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

Upvotes: 1

Views: 1403

Answers (2)

Rick James
Rick James

Reputation: 142296

The trick is to expect Throwable:

try {
    ...
    @eval(...);
    ...
} catch(Throwable $t) {
    ...
}

Upvotes: 1

Frank Forte
Frank Forte

Reputation: 2190

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()?

Upvotes: 1

Related Questions