如何在PHP的IF条件下使用字符串 [英] How to use string in IF condition in PHP
本文介绍了如何在PHP的IF条件下使用字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数组
我已使用爆破功能将数组转换为字符串,例如
i以下需要$ str变量作为if语句的条件,如下所示
I have array I have converted the array into string using implode function like below i need the $str variable as condition in if statement like below
[0]=>"1==1"
[1]=>"1==1"
[2]=>"2==1"
$str = "1==1 && 1==1 && 2==1";
if(1==1 && 1==1 && 2==1)
推荐答案
尽管这不是进行此类活动的好方法,但是您可以使用 eval()为此:
Though it is not a good way to do such activity, you can use eval() for this:
<?php
$str = "1==1 && 1==1 && 2==1";
$condition = eval("return $str;");
if($condition) {
echo "True";
} else {
echo "False";
}
演示用于虚假情况
演示真实情况
这篇关于如何在PHP的IF条件下使用字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文