php 字符串如何转换成if条件语句 例如:$condition = "2 == 2 && 3 == 5"; if ($condition) { echo 1; } 怎样把$condition 转换成if可识别的条件?直接这样判断会当成字符串常量,值为true------解决思路----------------------
if (eval("return $condition;")) { ------解决思路----------------------
引用:
<br /> $condition = "2<=2 && 2>=1 && (snb === snb <br><font color='#FF8000'>------解决思路----------------------</font><br> snb === hfu)";<br /> if (eval("return $condition;")) {<br /> echo 1;<br /> }else {<br /> echo 2;<br /> }<br /> <br /> if (2<=2 && 2>=1 && ('snb' === 'snb' <br><font color='#FF8000'>------解决思路----------------------</font><br> 'snb' === 'hfu')) {<br /> echo 3;<br /> } 呃 第一段的$condition和第二段if里面的内容是等价的吧 只是一个是字符串,一个是正常的if条件。 第二段运行没有问题的啊 第一段调用eval()会报一个常量未定义的错误,这里应该要转义?
你的本意是('snb' === 'snb'
------解决思路---------------------- 'snb' === 'hfu') 但是 你却写成(snb === snb
------解决思路---------------------- snb === hfu) 连$符号都没有的变量,自然被理解成常量了,然而你并没有定义常量。
------解决思路---------------------- 用变量转化一下,否则snb和hfu会被解释成常量。
$snb="snb"; $hfu="hfu"; $condition = '2<=2 && 2>=1 && ($snb === $snb ------解决思路---------------------- $snb === $hfu)'; if (eval("return $condition;")) { echo 1; }else { echo 2; }
Statement: The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn