Order in conditional statements

Possible duplicate:
php false place in condition

I noticed that a lot of PHP code uses conditional expressions such as CONST == VARIABLE. I grew up with syntax always formulated in reverse order. 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 }
+5
source share
2 answers

This is to prevent a common typo between ==and =, known as the iodine condition. Consider the following:

if( false = $foobar) {

This will result in an error, catching what will be considered an error, since you cannot assign anything to false. On the contrary:

if( $foobar = false) { 

This is a valid syntax and this is a pretty simple mistake.

if( $foobar == false), .

+8

All Articles