Given:
if ($variable = get_variable('variable')) {
// ...
}
The *$variable = get_variable('variable')* throws an 'Assignment in condition' warning in Zend Studio. I understand what the warning means, but does anyone know what the rationale behind it is? Is it merely coding conventions, a matter of readability, etc.?
This is a very common warning issued by IDEs/compilers in most languages that allow this construct: since = (assignment) and == (comparison) are very similar, and comparison is more common within an if statement, the warning is just there to let you know that you may have put in an assignment by mistake where you really intended a comparison.
It does this because:
if ($variable = get_variable('variable')) {
// ...
}
is very close to:
if ($variable == get_variable('variable')) {
// ...
}
The former is not exactly a good practice to get into. Zend Studio assumes that you are more likely to have meant the latter case, so it warns you about this. Not to say that this isn't a useful tool. It is usually more acceptable in a while loop, for reading a file line by line (while there is still a line to read). The problem is that it is hard to quickly pick out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With