Is it just me or is it just odd to have the precedence of assignment be higher than any other operator? Example in PHP I just came across:
function test($param1) {
$result = TRUE;
for ($i = 0; $i <; strlen($param1); $i++) {
if (!(ord($param1[$i]) >= 65 && ord($param1[$i]) <=90)) {
$result = $result && FALSE;
}
}
return $result;
}
The intention when calling this function is to check whether a string has all characters in the range A-Z (If there are better ways it would be great to hear about them). The key part being the $result = $result && FALSE
which I had thought would evaluate the right side to FALSE
then assign $result
that value.
But No. This little bug/feature took some tracking down.
It appears that the the assignment of $result = $result
is performed first then to no-one the operation TRUE && FALSE;
is carried out.
I actually had to give explicit direction to say $result = ($result && FALSE);
which does seem very bizarre.
Anyway, I don't see how this is particularly useful 'feature' to have? Any ideas or am I missing something really basic?