How to convert a php string to an if condition statement, for example, $ condition = "2 = 2 & 3 = 5 ";
If ($ condition ){
Echo 1;
}
How can we convert $ condition into conditions that can be recognized by if? This can be regarded as a string constant and the value is true.
Reply to discussion (solution)
if (eval("return $condition;")) {
$condition = "2<=2 && 2>=1 && (snb === snb || snb === hfu)";if (eval("return $condition;")) {echo 1;}else {echo 2;}
Eval () can solve the case of pure numbers. if there are English letters in the string, an error will be reported. what if it is the above situation?
2 <= 2 & 2> = 1 & (snb = snb | snb = hfu)
It is not a legal php conditional expression. do not say eval, that is, errors are reported if you write them directly in if.
$condition = "2<=2 && 2>=1 && (snb === snb || snb === hfu)";if (eval("return $condition;")) {echo 1;}else {echo 2;}if (2<=2 && 2>=1 && ('snb' === 'snb' || 'snb' === 'hfu')) {echo 3;}
Er, the $ condition in the first section is equivalent to the if content in the second section. it is just a string and a normal if condition.
The second stage is running normally.
When eval () is called in the first section, an undefined constant error is returned. Should we escape it here?
$condition = "2<=2 && 2>=1 && (snb === snb || snb === hfu)";if (eval("return $condition;")) {echo 1;}else {echo 2;}if (2<=2 && 2>=1 && ('snb' === 'snb' || 'snb' === 'hfu')) {echo 3;}
Er, the $ condition in the first section is equivalent to the if content in the second section. it is just a string and a normal if condition.
The second stage is running normally.
When eval () is called in the first section, an undefined constant error is returned. Should we escape it here?
Your intention is ('snb' = 'snb' | 'snb' = 'hfu ') but you write (snb = snb | snb = hfu) variables that do not even have the $ symbol. they are naturally understood as constants, but you have not defined constants.
Use a variable to convert it. otherwise, snb and hfu will be interpreted as constants.
$snb="snb";$hfu="hfu"; $condition = '2<=2 && 2>=1 && ($snb === $snb || $snb === $hfu)'; if (eval("return $condition;")) { echo 1; }else { echo 2; }
Er, it has been solved. thank you!
Er, it has been solved. thank you!
Paste the post.