Page 1 of 1

Evaluating Variables as IF conditions

Posted: Tue Jan 02, 2007 5:02 am
by Sherif
onion2k | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi... 
I've been struggling to find a solution to a situation that has me stumped.

The application I am developing needs to store 'rules' in a database that are then evaluated within the code.
(i.e. the user can write custom rules)

What i want to do is this:

if ($rule){Do something}

Where $rule is an if condition that I retrieve from the database and will look something like this - i.e. ' $this->status=="valid" and ($otherObj->emailsend=="yes" or $this->post=="yes") '



I tried it in test code (below), (at least how I [i]hoped[/i] it would work),  but it always returns 'pending':

Code: Select all

$status="someVal";
$x="$status=='Pending'";

if ($x){echo "pending";}
else{echo "Not-pending";}

Can someone out there help?


onion2k | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jan 02, 2007 5:10 am
by onion2k
In your code as it stands $x is just a string. If you do if($x){ it'll be basically the same as saying if(isset($x)){ ... which is true. You'll need to use eval() (lazy, potential security hole), or write a system that stores the rules in your own syntax and a little parser to turn them into something that can be evaluated (tricky, but cool if done right).

Posted: Tue Jan 02, 2007 6:27 am
by Sherif
Many thanks, onion2k, and sorry about the syntax tags :oops: ( it's my first day )

Using

Code: Select all

eval("if(" . $x. "){echo 'pending';}");
did the job.

You are right about security, although in this case the rules can only be written by the Admin user, and will be validated before entry into database, (i.e. check valid options, remove quotes etc.), so I think I can get away without writing my own parser.

Many thanks for such a quick response.

Posted: Tue Jan 02, 2007 6:47 am
by Chris Corbyn
Sherif wrote:Many thanks, onion2k, and sorry about the syntax tags :oops: ( it's my first day )

Using

Code: Select all

eval("if(" . $x. "){echo 'pending';}");
did the job.

You are right about security, although in this case the rules can only be written by the Admin user, and will be validated before entry into database, (i.e. check valid options, remove quotes etc.), so I think I can get away without writing my own parser.

Many thanks for such a quick response.
This might read a little cleaner:

Code: Select all

$condition = '$status == "Pending"';
if (eval("return (" . $condition .");"))
{
    echo "Pending";
}

Posted: Tue Jan 02, 2007 8:09 am
by Sherif
Thanks d11wtq,

That is indeed better, and it means that the 'else' clause can be outside the eval().
Clearly I have much to learn..