Created
December 15, 2011 16:37
-
-
Save notbenh/1481769 to your computer and use it in GitHub Desktop.
I'm wondering why an assignment of the iterator does not cause an infinite loop here?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* I know the issue here is the use of the assignment rather then equality, though | |
I would think that this would lead to an infinite loop as $c should be reset to | |
4 for every instance of the $c loop after $r = 2 ... right? But that's not what | |
seems to happen? Any one have any insight? | |
NOTE: why does the else clause never get tripped, if $c==1? | |
*/ | |
echo '<pre>'; | |
for($r=0; $r<15; $r++){ | |
for($c=0; $c<10; $c++){ | |
if(($r<2) || ($c=4||$c=5) ){ | |
echo "[$r,$c]"; | |
} | |
else { | |
echo '______'; | |
} | |
} | |
echo "\n"; | |
} | |
echo '</pre>'; | |
/* | |
<pre>[0,0][0,1][0,2][0,3][0,4][0,5][0,6][0,7][0,8][0,9] | |
[1,0][1,1][1,2][1,3][1,4][1,5][1,6][1,7][1,8][1,9] | |
[2,1] | |
[3,1] | |
[4,1] | |
[5,1] | |
[6,1] | |
[7,1] | |
[8,1] | |
[9,1] | |
[10,1] | |
[11,1] | |
[12,1] | |
[13,1] | |
[14,1] | |
</pre> | |
*/ | |
?> |
nevermind, see your comment now. looking further.
ah, it's because of operator precedence! it's running the right side as $c = (4 || $c=5) ... or similar. $c ends up being true if you dump just after the conditional check.
wrap parens around ($c=4) and ($c=5) to see the infinite loop
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
should ($c=4||$c=5) be using double-equals?