Conditional expression doubts in while loop

Source: Internet
Author: User
while ($i = 'AAA' && $j = 'BBB') {    var_dump($i, $j);    sleep(3);}
输出结果bool(true)string(3) "BBB"
-------------------------------------------------------------在写一个后台监听程序的时候使用了while循环,于是纠结了一下`while`中的条件表达式
    1. For the above code I expected output AAA BBB But why is it true BBB

    2. Look at this piece of code.

      while($ret = 100) {    var_dump($ret) // output:100}

      In this case, the conditional expression in parentheses $ret=100 is how the result is true or is false . I'm thinking of 100 assigning the value to the variable first $ret , and then the $ret boolean value to the result.

      I hope you will help us with doubts. Thank you, sir.

Reply content:

while ($i = 'AAA' && $j = 'BBB') {    var_dump($i, $j);    sleep(3);}
输出结果bool(true)string(3) "BBB"
-------------------------------------------------------------在写一个后台监听程序的时候使用了while循环,于是纠结了一下`while`中的条件表达式
    1. For the above code I expected output AAA BBB But why is it true BBB

    2. Look at this piece of code.

      while($ret = 100) {    var_dump($ret) // output:100}

      In this case, the conditional expression in parentheses $ret=100 is how the result is true or is false . I'm thinking of 100 assigning the value to the variable first $ret , and then the $ret boolean value to the result.

      I hope you will help us with doubts. Thank you, sir.

if (($i = 'AAA') && ($j = 'BBB')) {    var_dump($i, $j);}

Note the && priority level

Operator logic problem, price brackets are on the square.

while (($i = 'AAA') && ( $j = 'BBB')) {    var_dump($i, $j);// true bbbbb    sleep(3);}

    1. The comma operator causes output BBB, v=1,2 at this point, V is 2
      2. Dead Loop,. Convert to Boolea can ret = ret && true

  • 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.