Doubts about the conditional expression in the while loop

Source: Internet
Author: User
{Code ...} {code ...} {code ...} for the above code, I expected to output AAABBB, but why is it trueBBB? READ this code {code ...} here, how does the conditional expression $ ret100 in brackets determine whether the result is true or false. What I want is to set the value 100 first...
while ($i = 'AAA' && $j = 'BBB') {    var_dump($i, $j);    sleep(3);}
Output result bool (true) string (3) "BBB"
--------------------------------------------------------------- The while loop is used when writing a background listener, so the conditional expression in the 'While 'is entangled.
  1. For the above code, I expected to output AAA BBB, but why is it true BBB?

  2. Read this code

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

    The conditional expression in brackets$ret=100How is the result returned?trueOrfalse. What I want is100This value is assigned$retVariable, and then$retReturns a boolean value.

    I hope you can help us solve this problem. Thank you.

Reply content:
while ($i = 'AAA' && $j = 'BBB') {    var_dump($i, $j);    sleep(3);}
Output result bool (true) string (3) "BBB"
--------------------------------------------------------------- The while loop is used when writing a background listener, so the conditional expression in the 'While 'is entangled.
  1. For the above code, I expected to output AAA BBB, but why is it true BBB?

  2. Read this code

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

    The conditional expression in brackets$ret=100How is the result returned?trueOrfalse. What I want is100This value is assigned$retVariable, and then$retReturns a boolean value.

    I hope you can help us solve this problem. Thank you.

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

Note:&&Priority

For operator logic problems, the price bracket is Ouke.

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

  1. The comma operator causes the output bbb, V = 1, 2 at this time, v is 2
    2. endless loops ,. Conversion to boolea can be 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.