June 19th, 2013, 11:23 AM
key() returns null when it hits the end of the array. If you put an assignment inside a control structure's condition, whatever got assigned is used as the check. PHP's atomic operations, themselves, have boolean value. You can say:
$a = ($b = key($c);
$a and $b would be identical in this scenario. This behavior is often used as a shortcut to replace:
while ( null !== ( $a = key($c) ) )
Since key() returns null when it hits the end of the array, the result of the assignment will be null, which in PHP is == false (thanks to loose typing) and the while loop will halt when the array runs out. Technically what you have is valid, but as you've discovered it's confusing.
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question
or you're a Help Vampire.
Trying to argue intelligently? Please read this.