Questions about conditional expressions in while loop

WBOY
Release: 2016-08-04 09:22:18
Original
1960 people have browsed it

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

  2. Look at this code

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

    How does the conditional expression$ret=100in the brackets here result intrueorfalse. What I want is to first assign the value100to the$retvariable, and then perform a Boolean conversion on$retto get the result.

    Hope everyone can help clear up the confusion. Thank you.

Reply content:

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

  2. Look at this code

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

    How does the conditional expression$ret=100in the brackets here result intrueorfalse. What I want is to first assign the value100to the$retvariable, and then perform a Boolean conversion on$retto get the result.

    Hope everyone can help clear up the confusion. Thank you.

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

Attention&&Priority

Operator logic problem, price brackets are wrong

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

  1. The comma operator causes the output bbb, V=1,2. At this time, v is 2
    2. An infinite loop. Convert to boolea can be ret = ret && true

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!