Jump to content

Archived

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

aphex_

Dynamic operators in If conditionals *SOLVED*

Recommended Posts

I am attempting to make an operator in an if conditional
a variable like so:

$x=1;
$y=2;
$operator='!=';

if ($x $operator $y) {
echo "Not equal!\n";
}

But obviously that doesnt work.
Does anyone know if something like this is achievable and how? [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]
Any help would be appreciated.

Thanks,

Aphex_

Share this post


Link to post
Share on other sites
You could look at using the [a href=\"http://www.php.net/eval\" target=\"_blank\"]eval()[/a] function.

Tell us in more detail why you want to do this and we might be able to suggest an alternative method.

Ken

Share this post


Link to post
Share on other sites
Well, I will be storing the operators in database fields in a table.
This table will also store the column name that is being checked
and the variable it is being compared with.
Basically, this will allow end users to write rules so that if
text fields meet certain conditions they are notified by email.

A real world example would be so that if a car sale is made for
over $50,000 the boss is notified.
In this case the column name might be sale_amount and the
comparison variable $50,000. The operator database field
obviously would be '>=';
So when a new row is injected in the database recording a car sale,
the rules table is checked, and if the sale amount is over 50k, the boss is notified.

Share this post


Link to post
Share on other sites
I just wanted to thank the last responder for their suggestion to use the eval() function.
I have managed to get it partially working with this and that has been the best
answer anyone has given me to this question so far.

-Aphex

Share this post


Link to post
Share on other sites
if you still have problems (working partially) you could post your current code to get further help

Share this post


Link to post
Share on other sites
Here is what I am doing and it is working -

$x='somestring';
$y='someotherstring';
$operator = '==';

$condition="if ($x $operator $y) {
echo "We gotta match!\n";
}";
eval("$condition");

Thanks Again!

Share this post


Link to post
Share on other sites
[!--quoteo(post=374566:date=May 16 2006, 11:09 PM:name=aphex_)--][div class=\'quotetop\']QUOTE(aphex_ @ May 16 2006, 11:09 PM) [snapback]374566[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Here is what I am doing and it is working -

$x='somestring';
$y='someotherstring';
$operator = '==';

$condition="if ($x $operator $y) {
echo "We gotta match!\n";
}";
eval("$condition");

Thanks Again!
[/quote]
I think you meant:

$condition="if ($x $operator $y) {
echo \"We gotta match!\n\";
}";

otherwise you'd get a syntax error.

Share this post


Link to post
Share on other sites

×

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.