Why do some coding rules change the order of conditions in PHP if / else?

Some coding guidelines indicate that you should put the variable that you are testing at the end of the condition:

// Incorrect
if($isSomething === FALSE) { // Do something }

// Correct
if(FALSE === $isSomething) { // Do something }

I know that some programmers have a bad habit of initializing variables in such conditions:

if($results = $db->getResults() { // Do something if results exist }

Therefore, the only reason I could suggest that this contradictory rule is to prevent incorrect reinvaluation if you accidentally use only one equal sign (=) instead of two in PHP.

Are there any other reasons?

+3
source share
1 answer

You cannot accidentally assign a value if you want to compare. This makes it easier to find small errors.

if (FALSE = $isSomething) ,
if ($isSomething = FALSE) true, , , .

+5

All Articles