Possible Duplicate:
php false place in condition
I have noticed that a lot of PHP code uses conditional statements like CONST == VARIABLE. I grew up with the syntax always articulated in reverse. Is there a reason for this structure?
Example:
// this is the way I see it most typically represented in PHP
if ( false == $foobar ) { // do this }
// this is the way I normally do it
if ( $foobar == false ) { // do this }
This is to prevent a common typo between
==and=, known as a yoda condition. Consider the following:This would result in an error, catching what would be considered a bug, since you cannot assign anything to
false. On the contrary:This is valid syntax, and is quite an easy mistake to make.
However, I typically prefer the
if( $foobar == false)syntax, as unit tests should be able to catch these programmatic mistakes.