Jump to content

Dynamic operators in If conditionals *SOLVED*


aphex_

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_
Link to comment
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
Link to comment
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.
Link to comment
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
Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.