如何使用php中的字符串内的条件
问题描述:
我想知道是否有办法使用这样的东西:
I want to know if there is a way to use something like this:
$t1=1;
$t2=2;
$t3="$t1>$t2";
if($t3)
echo "Greater";
else
echo "Smaller";
这将评估为真,因为$ t3是一个字符串,但这是错误的!
This will evaluate to true as $t3 is a string, but that's wrong!!!
那么有没有办法在字符串中包含if条件。
So is there a way to include the if condition inside string.
我听说我们可以使用eval:
PHP - 如果字符串内的条件
I heard that we can use eval for this: PHP - if condition inside string
但是这怎么可能???
But how is this possible???
答
看起来它应该可行,给定PHP 评估页面。
This looks like it should work, given the PHP eval page.
$t1=1;
$t2=2;
$t3="return $t1>$t2;";
if(eval($t3))
echo "Greater";
else
echo "Smaller";