I'm trying to use the eval function for php. but I'm stuck in handling the parse error. like considering if I have edge cases like 1.. or 1++ if gives me parse error:syntax error, .....
anyone knows how to handle syntax error or how to bypass the error message? I want to give a better error message.
also is it possible to store the error message to a variable?
TIA
From the manual
As of PHP 7, if there is a parse error in the evaluated code,
eval()throws aParseErrorexception. Before PHP 7, in this caseeval()returnedFALSEand execution of the following code continued normally. It is not possible to catch a parse error ineval()usingset_error_handler().
Instead use this:
<?php
try {
eval('will cause error');
} catch (ParseError $e) {
echo 'Caught exception: '.$e->getMessage()."\n";
}
https://3v4l.org/1giOS
$response = @eval($string);
if (error_get_last()){
echo 'Show your custom error message';
//Or you can
print_r(error_get_last());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With