How to convert a php string to an if condition statement

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.