php |关于条件语句的简单问题

php |关于条件语句的简单问题,php,conditional,Php,Conditional,我是php新手,希望有这样一个条件语句: if ($foo != 'Any') { $condition .= '$this_foo == $foo &&';} if ($bar != 'Any') { $condition .= '$this_bar == $bar &&';} if ($txt != 'Any') { $condition .= '$this_txt == $txt &&';} $condition .= '$num1 >

我是php新手,希望有这样一个条件语句:

if ($foo != 'Any') { $condition .= '$this_foo == $foo &&';}
if ($bar != 'Any') { $condition .= '$this_bar == $bar &&';}
if ($txt != 'Any') { $condition .= '$this_txt == $txt &&';}
$condition .= '$num1 > 0 && $num2 < 1000';

if (...[php would parse the $condition variable])
{
  someoperations
}
if($foo!='Any'){$condition.='$this\u foo==$foo&&'}
如果($bar!='Any'){$condition.='$this\u bar==$bar&&'}
如果($txt!='Any'){$condition.='$this_txt===$txt&&'}
$condition.='$num1>0&$num2<1000';
if(…[php将解析$condition变量])
{
一些手术
}
if语句解析变量$condition的正确语法是什么?因此,条件语句依赖于其他变量,以防止长嵌套条件语句


提前谢谢

我相信你想要的是

if (eval("return " . $condition)) {...}

如果解析失败,请确保检查是否存在错误的情况。

好吧,这不完全是解析,但您可以在执行代码时评估您的条件

$condition = true;
if ($foo != 'Any') { $condition = $condition && ($this_foo == $foo);}
if ($bar != 'Any') { $condition = $condition && ($this_bar == $bar);}
if ($txt != 'Any') { $condition = $condition && ($this_txt == $txt);}
$condition = $condition && ($num1 > 0 && $num2 < 1000);

if ($condition)
{
  someoperations
}
让我们假设
$this\u foo==$foo
$num1==45
$num2==2300

$condition = true && true && (true && false);
$condition = false;

您的if将不会执行。

谢谢!它帮助了我。等我有了名声,我会把这个投上去的。好了。我昨天才加入这里,所以我是个新手
$condition = true && true && (true && false);
$condition = false;