Use operator inside string.

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
fh2nov82
Forum Newbie
Posts: 2
Joined: Thu Jan 28, 2010 12:59 am

Use operator inside string.

Post by fh2nov82 »

Hi there folks. Not sure if its possible, I'm trying to do a comparison of two values. However, the comparison operator is stored in a string. Here's what I'm trying to do.

Code: Select all

$oper = '>='; //given to me, op can be '<', '<=', '==', '>=', or '>'
$value1 = 10;
$value 2 = 15;
 
if($value1 $oper $value2) {
    statements...
}
I figured the above wouldn't work, but I'm wondering if there may be something as simple that can be done. If not I image the simplest course of action would be if..else if..else checks for each possible operator then doing the check I want to make.

Code: Select all

if($oper == '<') {
    if($value1 < $value2) {
        statements...
    }
} else if($oper == '<=') {
    if($value1 <= $vaule2) {
.....and so on
Thanks in advance for any assistance.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Use operator inside string.

Post by requinix »

Can you guarantee that
- $value1 is always a number?
- $value2 is always a number?
- $oper is always one of those five operators?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Use operator inside string.

Post by AbraCadaver »

Several ways to do it, probably all will need eval():

Code: Select all

$oper = '>=';
$value1 = 10;
$value2 = 15;
 
eval('$comparison = ' . $value1 . $oper . $value2 .';');
 
if($comparison) {
    //statements...
}
 
//or
 
$comparison = $value1 . $oper . $value2;
 
if(eval("return $comparison;")) {
    //statements...
}
 
I prefer this style:
 

Code: Select all

$comparison = eval("return $value1 $oper $value2;");
 
if($comparison) {
    //statements...
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
fh2nov82
Forum Newbie
Posts: 2
Joined: Thu Jan 28, 2010 12:59 am

Re: Use operator inside string.

Post by fh2nov82 »

Your preferred style seems to have worked AbraCadavar, thank you very much. :)

Though I had to do it like this:
$comparison = eval('return '.$value1.' '.$oper.' '.$value2.';');

Thank you kindly for your assistance.

Thank you for your reply as well tasairis. Yes $oper will always be one of the given 5 operators, and both values are form inputs which I typecast to int before inserting into a database(along with a chosen operator). I'm running the comparison after extracting them from the database and hoping that they will both be integers ;)
Post Reply