3

Is this possible?

$var_1 = 1;
$var_2 = 10;
$comparison = '>';

if($var_1 $comparison $var_2) {
    // do something...
}

The syntax right now is not valid but is there a way to do this?

Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • 1
    Check out this question: http://stackoverflow.com/questions/2552052/variable-operators-in-php – tplaner Jun 02 '11 at 14:29

6 Answers6

9

Not natively (except of course with eval). You will need to evaluate the various expressions yourself. Build a wrapper function and use a switch statement. Or for non-expression comparisons a simple map:

$var_1 = 1;
$var_2 = 10;
$comparison = '>';

$map = array(
    ">" => $var_1 > $var_2,
    "<" => $var_1 < $var_2,
    "==" => $var_1 == $var_2,
    "!=" => $var_1 != $var_2,
);

if($map[$comparison]) {
    // do something...
}
mario
  • 144,265
  • 20
  • 237
  • 291
5

It is possible with

if (eval("{$var_1} {$comparison} {$var_2}")) {

But eval is strongly discouraged, so do this only if you can't achieve desired result otherwise

EDIT As @mario noted, until PHP 5.3 this doesn't work. In 5.3 it does. So for older versions it should be

if (eval("return {$var_1} {$comparison} {$var_2};")) {
mkilmanas
  • 3,395
  • 17
  • 27
  • if you do use eval, and `comparison` is user input, make sure to check it against a white list of possible comparisons – Earlz Jun 02 '11 at 14:30
  • 1
    Your eval is not going to function without `return` and the closing `;` semicolon. `eval` only works on statements, not expressions. – mario Jun 02 '11 at 14:31
3

You probably should use eval(). As like in other programming languages, PHP’s eval() is also powerful and dangerous — be careful.

If you write something like the following code:

if (eval("$var_1 $comparison $var_2")) {
    // ...
}

It won’t work. Because PHP’s eval() doesn’t evaluate an expression, but just execute a statement. So you should do like:

eval("\$condition = $var_1 $comparison $var_2;"); // semicolon required!
if ($condition) {
    // ...
}

It would work well except $var_1 or $var_2 is not number. For example, if $var_1 is a string like 'hello', the code eval() executes becomes:

eval("\$condition = hello > 2;");

To avoid breaking this code, you should escape dollar signs of $var_1 and $var_2:

eval("\$condition = \$var_1 $comparison \$var_2;"); // don't escape $comparison's.
minhee
  • 5,688
  • 5
  • 43
  • 81
0

make the string a conditional, returning a value if true or false

short form if statement

$var_1 = 1;
$var_2 = 10;
$comparison = '>=';

eval("\$b=$var_1 $comparison $var_2?1:0;");

if ($b) {
//do something
}
Robert
  • 1
0

I'm not sure if this is possible without the hackish use of an eval statement. You could of course wrap simply test what comparison is being used within another if statement.

if($comparison == '>' && $var_1 > $var_2)
    //Do something
elseif($comparison == '<' && $var_1 < $var_2)
    //Do something, and so on

The eval method should work too if you build you code into an a string.

if(eval("$var_1 $comparison $var_2")) //Do something

But the first method is probably far more preferable.

Sam Becker
  • 19,231
  • 14
  • 60
  • 80
0

You could also create a few functions like isGreaterThan($arg1, $arg2), isLessThan($arg1, $arg2), and then store the name of the function you want in the variable $comparison. Then you would use call_user_func_array() when you want to use it. Like so :

function isGreaterThan($val1, $val2) {
    return $val1 > $val2;
}

/////////////

$var_1 = 1;
$var_2 = 10;
$comparison = "isGreaterThan";

if(call_user_func_array($comparison, array($var_1, var_2))) {
    //do something
}

Sounds like you've got a few options. Hope this helps.

Ian
  • 3,806
  • 2
  • 20
  • 23