My supervisor at work told me to slightly alter some PHP code from an existing ‘view’ to put into the new ‘view’ we were working on. I could not understand what he had done (his code) until I realized (after staring) that he had assigned a variable inside the brackets where the condition for the ‘loop’ usually sits.
usually a while loop has a condition that has to be met inside the bracket i.e.
while ($i <= 10) {
echo ‘hello number’.$i;
i++;
}
His code iterated over an array, assigning each iteration to a variable and then performing an operation i.e.
$iCount=0;
while($somevariable=$someArray[$iCount][$someValue]) {
echo ‘$somevariable’;
$iCount++;
}
This absolutely did my head in. I thought he meant ‘==’, not’=', but it was an assignment operator and the code works. Nowhere have I found any mention of PHP working like this. How does this make sense? How is ‘assigning an array key’s value’ a CONDITION? does ‘the task of assigning the variable’ return true when tested for true or false?
Thanks