Reputation: 1181
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
Reputation: 142296
The trick is to expect Throwable
:
try {
...
@eval(...);
...
} catch(Throwable $t) {
...
}
Upvotes: 1
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