如何查看if
来自数据库列的语句,如 $a > $b
或 $a == $b
或 $a != $b
或 $a < $b
我从 $row['condition']
中的数据库中获取数据
$row['condition'] = '$a == $b';
或者
$row['条件'] = '100 == 10';
在 if 语句中我想检查
if($row['condition']){
echo 'true';
}else{
echo 'false';
}
我要输出
false
最佳答案
这样做的一种方法是使用 eval()功能。但正如手册所述,这非常危险:
Caution:
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.1
也就是说,可以使用这段代码:
$row['condition'] = '$a == $b';
$a=10;
$b=20;
// Evaluate the condition using "return your_condition;"
if (eval( 'return ' . $row['condition'] . ';' ))
{
echo "true";
}
else
{
echo "false";
}
测试一下 here .
关于php - 如何从数据库列中检查 `if` 语句,如 '$a > $b',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43637376/