5

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

4
  • 2
    Why the hell do you use eval on code that was apparently not written by you? Commented Apr 10, 2012 at 8:14
  • There has to be a better way than using eval on server side, or even client side for that matter... Commented Apr 10, 2012 at 8:15
  • well, this is the project specs, and wont be used for real-world purposes. Commented Apr 10, 2012 at 8:31
  • When someone provides a elaborate answer (which is correct) you should do your tiny duty and accept it. Commented Jan 24, 2023 at 13:31

3 Answers 3

20

From the manual

As of PHP 7, if there is a parse error in the evaluated code, eval() throws a ParseError exception. Before PHP 7, in this case eval() returned FALSE and execution of the following code continued normally. It is not possible to catch a parse error in eval() using set_error_handler().

Instead use this:

<?php

try {
    eval('will cause error');
} catch (ParseError $e) {
    echo 'Caught exception: '.$e->getMessage()."\n";
}

https://3v4l.org/1giOS

Sign up to request clarification or add additional context in comments.

2 Comments

This should be the new selected answer
I tried to catch these errors with the usual generic "Exception $e" This one works.
12
$response = @eval($string);
if (error_get_last()){
    echo 'Show your custom error message';
    //Or you can 
    print_r(error_get_last());
}

2 Comments

Only after like 30 minutes of trying to track it down did I find this and it actually helped me catch the error. In my defense, this wasn't my code. I was fixing someone else's and they used eval like it was going out of style!
error_get_last() does not really work like this. In fact, it returns null only if there is absolutely no error in all the script, even outside the eval'd string. You can overwrite the last error by causing one yourself, like @trigger_error('MY_LAST_ERROR');, and later check if the message in the array returned by error_get_last is 'MY_LAST_ERROR', but this only works if the currently set_error_handler returns false.
2

From the manual:

If there is a parse error in the evaluated code, eval() returns FALSE and execution of the following code continues normally. It is not possible to catch a parse error in eval() using set_error_handler().

But as you won't be calling eval on arbitrary code (right?), this shouldn't be a problem.

4 Comments

yes, I used preg_match to validate it. is there any way to change the error message though?
@ordinaryman09: You can't use preg_match to validate arbitrary PHP code (AFAIK). The fact that you have "edge cases" demonstrates that.
yes, it's not perfect, so I need to handle the edge cases just by displaying the proper error message.
@ordinaryman09: This is very dangerous. The fact that your validation isn't perfect means that you can potentially execute arbitrary PHP code. You need to find a better solution than using eval. But anyway, if you browse through the comments on the manual page I linked to, someone came up with a "solution", which is using fork before running eval. I don't know whether it works, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.