Jump to content


Photo

Dynamic operators in If conditionals *SOLVED*


  • Please log in to reply
6 replies to this topic

#1 aphex_

aphex_
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 May 2006 - 11:27 PM

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_

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 May 2006 - 11:39 PM

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

#3 aphex_

aphex_
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 May 2006 - 02:37 AM

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.

#4 aphex_

aphex_
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 May 2006 - 02:56 AM

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

#5 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 17 May 2006 - 05:18 AM

if you still have problems (working partially) you could post your current code to get further help

#6 aphex_

aphex_
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 May 2006 - 06:09 AM

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!

#7 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 17 May 2006 - 06:12 AM

[!--quoteo(post=374566:date=May 16 2006, 11:09 PM:name=aphex_)--][div class=\'quotetop\']QUOTE(aphex_ @ May 16 2006, 11:09 PM) View Post[/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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users