sofia Posted May 27, 2009 Share Posted May 27, 2009 Hi, I'm developing an app where I have to parse hundreds of expressions. Eg. I have an xml that has the following: <item> <rule>ratio->L > 10 && ratio->T > 2</rule> <text>text to display if expression is true..</text> <item> If an expression is true, then the code should display the corresponding text. Some text elements contain loads of text.. I have managed to abstract some of these expressions without eval but not all. I first tried eval and then gave up, but now due to the sheer amount of expressions I have to parse, I wanna try it again. First, I realize eval is evil but I do have control over the expressions - I'm the one who puts them there anyway, they can't be changed by the client. So let's say for simplicity's sake I have the following code: class A { function __construct() { } function init() { $this->ratio['L'] = 10; } } $str = '$a->ratio["L"] >= 1'; $a = new A(); $a->init(); if(eval($str)){ echo 'not ok';//this will display the text element in the xml.. }else{ echo 'ok'; } But this always gives me the following error: Parse error: syntax error, unexpected $end parser.php(23) : eval()'d code on line 1 I don't want to put the whole thing in there eg. <rule>if($this->ratio->L > 10 && $this->ratio->T > 2){ return 'text to be returned'; }</rule> because well, it just seems even more ugly/hackish.. Any ideas? Thanks Best, Sofia Link to comment https://forums.phpfreaks.com/topic/159852-eval-uating-expressions/ Share on other sites More sharing options...
Ken2k7 Posted May 27, 2009 Share Posted May 27, 2009 $str has to be PHP code, which means you need a semi-colon after the >= 1 because you're evaluating it as a statement. $str = '$a->ratio["L"] >= 1;'; Try that. Link to comment https://forums.phpfreaks.com/topic/159852-eval-uating-expressions/#findComment-843094 Share on other sites More sharing options...
sofia Posted May 27, 2009 Author Share Posted May 27, 2009 Thanks. It doesn't give me any errors now but it doesn't really parse the expression either, it always returns true - maybe it's just checking it's a string and so evaluates to true?? Any help? Link to comment https://forums.phpfreaks.com/topic/159852-eval-uating-expressions/#findComment-843379 Share on other sites More sharing options...
anupamsaha Posted May 27, 2009 Share Posted May 27, 2009 Thanks. It doesn't give me any errors now but it doesn't really parse the expression either, it always returns true - maybe it's just checking it's a string and so evaluates to true?? Any help? In the "init()" method, value is set to 10. And, 10 is always greater than 1 (as in the expression). Thats why, its always true. Does it help? Link to comment https://forums.phpfreaks.com/topic/159852-eval-uating-expressions/#findComment-843415 Share on other sites More sharing options...
thebadbad Posted May 27, 2009 Share Posted May 27, 2009 eval() returns NULL unless return is called within the evaluated code. And all you're doing is running 10 >= 1; which doesn't do anything. So to test if your expression is true, you'll need to put that inside an if construct, and return true if it evaluates as true. Something like: if(eval("if ($str) return true;")) Link to comment https://forums.phpfreaks.com/topic/159852-eval-uating-expressions/#findComment-843435 Share on other sites More sharing options...
sofia Posted May 27, 2009 Author Share Posted May 27, 2009 Thank you What i needed was the if and return statement inside the eval.. something like below.. $str = '$a->ratio["L"] >= 15'; $strif = 'if('.$str.'){ return true;}else{return false; }'; $a = new A(); $a->init(); if(eval($strif)){ echo 'bigger than 15'; }else{ echo 'not bigger than 15'; } Noobie at eval.. but now I understand thet it must evaluate 1 whole statement and not partial expressions.. got it! Thanks again Link to comment https://forums.phpfreaks.com/topic/159852-eval-uating-expressions/#findComment-843596 Share on other sites More sharing options...
redarrow Posted May 27, 2009 Share Posted May 27, 2009 as stated for loads of programmers that eval is a bad bit off stuff be careful. try not to use eval at all. Link to comment https://forums.phpfreaks.com/topic/159852-eval-uating-expressions/#findComment-843601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.