开发者

Build condition comparison for if statement

开发者 https://www.devze.com 2023-03-09 20:28 出处:网络
Is this possible? $var_1 = 1; $var_2 = 10; $compa开发者_Python百科rison = \'>\'; if($var_1 $comparison $var_2) {

Is this possible?

$var_1 = 1;
$var_2 = 10;
$compa开发者_Python百科rison = '>';

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

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


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...
}


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};")) {


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.


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.


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.


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消