Evaluating Variables as IF conditions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sherif
Forum Newbie
Posts: 3
Joined: Tue Jan 02, 2007 4:37 am

Evaluating Variables as IF conditions

Post 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]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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).
Sherif
Forum Newbie
Posts: 3
Joined: Tue Jan 02, 2007 4:37 am

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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";
}
Sherif
Forum Newbie
Posts: 3
Joined: Tue Jan 02, 2007 4:37 am

Post 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..
Post Reply