How PHP strings are converted to if condition statements

Source: Internet
Author: User
For example: $condition = "2 = = 2 && 3 = = 5";
if ($condition) {
Echo 1;
}
How do I convert a $condition to an if-recognizable condition? This judgment is treated as a string constant with a value of 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, the string has an English letter will be error, if it is above the situation what to do?

2<=2 && 2>=1 && (SNB = = SNB | | snb = = = Hfu)
itself is not a valid PHP conditional expression, do not say Eval, is that you write directly in if is to error.

        $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;}

Well, the first paragraph of the $condition and the second paragraph if the content is equivalent it is just a string, one is the normal if condition.
There's no problem with running the second paragraph.
The first call to Eval () will report a constant undefined error, which should be escaped 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;}

Well, the first paragraph of the $condition and the second paragraph if the content is equivalent it is just a string, one is the normal if condition.
There's no problem with running the second paragraph.
The first call to Eval () will report a constant undefined error, which should be escaped here?


Your intention is (' SNB ' = = = ' SNB ' | | ' SNB ' = = = ' Hfu ') but you write (SNB = = = SNB | | snb = = = Hfu) A variable that does not even have a $ symbol, is naturally understood as constant, yet you do not define a constant.

Convert them with variables, 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;    }

Uh, it's settled, thank you, big God.

Uh, it's settled, thank you, big God.

That's a knot.
  • Related Article

    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.