Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the rationale behind "Assignment in condition" warnings in Zend Studio IDE?

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.?

like image 249
webjawns.com Avatar asked Dec 02 '25 20:12

webjawns.com


2 Answers

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.

like image 199
casablanca Avatar answered Dec 04 '25 11:12

casablanca


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.

like image 43
Chris Laplante Avatar answered Dec 04 '25 10:12

Chris Laplante



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!