Jump to content

eval -uating expressions


sofia

Recommended Posts

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

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?

 

 

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

Thank you :D

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.