FALSE if语句

I'm looking for a good explanation for what is the differences/advantages to using which of the following:

if (!$whatever) or if ($whatever == FALSE)

if (!$whatever)

Just takes the value of $whatever and inverts it.

if ($whatever == FALSE)

Evaluates if $whatever is equal to FALSE

Most people would say if ($whatever == FALSE) is wrong because if you think about another situtation if ($whatever == TRUE) you can see that it is silly; You could just use if($whatever). When you do if ($whatever == FALSE) it feels like their is an extra unnessasary step of evaluating the comparison even though we shouldn't need to.

Their really is no advantage or disadvantage to doing it their way as they have the same effect, but other people you think you foolish to write if ($whatever == FALSE). ($whatever == FALSE feels unnecessarily complex and Rube Goldberg. Also if ($whatever == FALSE) is probably slower to execute, but it is negliable.

It is identical, however most programmers prefer to write !$value, because it is more concise.

Because of PHP's type juggling, these are identical.

Note: Both of these will return true for "falsey" values. Such as:

  • 0
  • null
  • array() // empty array
  • '' // blank string
  • FALSE

These all convert to boolean FALSE, which == FALSE (and !FALSE is TRUE).

Although the questions' answer seems obvious, perhaps it might be a good idea ellaborate:

if (boolean expression) construct checks whether the expression is equal to true.

$whatever = false;

!$whatever evaluates to:    
!($whatever) = true;

$whatever == false evaluates to:    
($whatever == false) = true;

if(true == true);

The major difference is that ! (not operator) inverts the boolean value (false -> true) and the if construct checks whether this inversion is equal to true.

The equality operator evaluates whether $whatever is equal to the value (not necessarily a boolean value) and the if construct checks if the equality expression is true.

TJHeuvel is correct that the use of the not operator is preferred in this case because it is more concise. Consider using the not operator on something not as trivial, for example:

if((ProgramState == ProgramState.Ready) == false) vs    
if (!(ProgramState == ProgramState.Ready)) vs    
if (ProgramState != ProgramState.Ready)

The latter being more concise (hence the not equal to operator).