1

what is the difference between these two expressions ,

if(false == $flag)

if($flag == false)

what is the benefit of this expression rather than normal initiation like this :

if($flag == false)
niaher
  • 9,460
  • 7
  • 67
  • 86
  • 3
    Using `if(false == $flag)`, you can prevent accidental assignments inside the `if` statement, but I think it's a matter of preference. – Amal Murali Dec 26 '13 at 07:39
  • 3
    Everyone I know would write that as `if(!flag)` – Ultimater Dec 26 '13 at 07:40
  • @Ultimater Why, you don't write `if (($a>$b) == true)`? – Cthulhu Dec 26 '13 at 07:42
  • You would. `($a>$b)` is also boolean, so by the same logic it should be `if(($a > $b) == true)` ;) – Amal Murali Dec 26 '13 at 07:45
  • 1
    Okay, found the duplicate. Here you go: [Coding Style - if ('constant' == $variable) vs. if ($variable == 'constant')](http://programmers.stackexchange.com/q/74086) – Amal Murali Dec 26 '13 at 07:46
  • in addition to the other answers for a detailed discussion (not particularly for php) , see here : http://stackoverflow.com/questions/271561/why-does-one-often-see-null-variable-instead-of-variable-null-in-c – gaurav5430 Dec 26 '13 at 07:50

1 Answers1

3

In first case, if(false == $flag), compiler warn you if you type accidentally = instead of ==. But this is not true in the second case, if($flag == false).
In second case if == is accidentally replaced by = then compiler do not show you any warning or error instead $flag = false will assign false to flag and the condition $flag = false will always be false.

haccks
  • 104,019
  • 25
  • 176
  • 264