Skip to content

Instantly share code, notes, and snippets.

@notbenh
Created December 15, 2011 16:37
Show Gist options
  • Save notbenh/1481769 to your computer and use it in GitHub Desktop.
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?
<?
/* 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>
*/
?>
@alanszlosek
Copy link

should ($c=4||$c=5) be using double-equals?

@alanszlosek
Copy link

nevermind, see your comment now. looking further.

@alanszlosek
Copy link

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