PHP: is empty($var) equivalent to !$var

the problem is that since !$vars is shorter than empty($vars) many of us will prefer the first way

You prefer the first one because it is a "shorter way"?
Shorter code does not mean better code, or faster scripts ;)

The speed of PHP functions and its various other behaviours is not determined by the length of the function name. It is determined by what PHP is actually doing to evaluate, action, and return results.

Besides that, don't choose methods based on length of code, choose methods based on scenario and best approach "for a given scenario".
Which is best depends on what you need, and there are other variable checks other than the two you mentioned (isset() for one).

but the problem is are they equivalent always

Not necessarily - see http://php.net/manual/en/types.comparisons.php

Or create a quick test script to see what PHP returns for your two scenarios.

You could also be initialising your variables in your framework (or, likely, stand alone script), which means the scenario changes, as could your question and approach you use.

It's all contextual as to which is the best.

As for the required answer.

Anyway, to answer your question, here are some tests:

(!$vars)

Example code:

if ( !$vars )
 {
  echo "TRUE";
 }
else
 {
  echo "FALSE";
 }

will return:
Notice: Undefined variable: vars in /whatever/ on line X
TRUE

However, if you initialise the var in your scripts somewhere first:

$vars = "";
$vars = NULL;
$vars = 0;

Any of the above will return:
[no PHP notice]
TRUE

$vars = "anything";

will return:
FALSE

This is because with the exclamation mark you are testing if the var is FALSE, so when not initialised with a string the test script returns TRUE because it is NOT FALSE.

When we initialise it with a string then the var NOT being FALSE is FALSE.

empty($vars)

Example code:

if ( empty($vars) )
 {
  echo "TRUE";
 }
else
 {
  echo "FALSE";
}

Not initialised/set at all, and all of the following:

$vars = "";
$vars = NULL;
$vars = 0;

will return:
TRUE

There is no PHP notice for using empty, so here we show a difference between the two options (and remember when I said the shortest code is not necessarily the "best"? Depends on the scenario etc.).

And as with the previous test:

$vars = "anything";

returns:
FALSE

This is the same with ( !$var ), you are testing IF EMPTY, and without the var being initialised at all, or with any "empty()" value: eg (""), or NULL, or zero (0), then testing if the var is empty is TRUE, so we get TRUE output.

You get FALSE when setting the var to a string because IS EMPTY = FALSE as we set it to something.


The difference is empty() does not throw a PHP notice when var is not defined, whereas (!$var) will.

Also, you may prefer it for being "shorter code", but if (!$var) isn't really much shorter/better looking than the widely used if (empty($var)).

But again, this depends on the scenario - PHP provides different options to suit different requirements.


No they are not equal

if(empty($var)){
  echo "empty used\n";
}


if(!$var){ # line no. 6
  echo "! used";
}

will output

empty used
Notice: Undefined variable: var in /var/www/html/test.php on line 6
! used

Following values are considered to be empty when using empty() function

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

As you can read in empty docs

empty() is essentially the concise equivalent to !isset($var) || $var == false.

Tags:

Php