while ($i = 'AAA' && $j = 'BBB') { var_dump($i, $j); sleep(3); }
输出结果 bool(true) string(3) "BBB"
------------------------------------------------------------- 在写一个后台监听程序的时候使用了while循环,于是纠结了一下`while`中的条件表达式
For the above code, I expected to output AAA BBB, but why is it true BBB
Look at this code
while($ret = 100) { var_dump($ret) // output:100 }
How does the conditional expression$ret=100
in the brackets here result intrue
orfalse
. What I want is to first assign the value100
to the$ret
variable, and then perform a Boolean conversion on$ret
to get the result.
Hope everyone can help clear up the confusion. Thank you.
while ($i = 'AAA' && $j = 'BBB') { var_dump($i, $j); sleep(3); }
输出结果 bool(true) string(3) "BBB"
------------------------------------------------------------- 在写一个后台监听程序的时候使用了while循环,于是纠结了一下`while`中的条件表达式
For the above code, I expected to output AAA BBB, but why is it true BBB
Look at this code
while($ret = 100) { var_dump($ret) // output:100 }
How does the conditional expression$ret=100
in the brackets here result intrue
orfalse
. What I want is to first assign the value100
to the$ret
variable, and then perform a Boolean conversion on$ret
to get the result.
Hope everyone can help clear up the confusion. Thank you.
if (($i = 'AAA') && ($j = 'BBB')) { var_dump($i, $j); }
Attention&&
Priority
Operator logic problem, price brackets are wrong
while (($i = 'AAA') && ( $j = 'BBB')) { var_dump($i, $j);// true bbbbb sleep(3); }
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