Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

For validating a variable value we can do

if(empty($var)){
}

OR

This will return true on empty string, 0 as number, false, null

if(!$var){
}

What is the difference between this two approaches, are them equivalent?

EDIT: Some examples where they behave different will be more pratical.

EDIT2: The only difference founded from answers is that the second will throw a notice if $var is undefined. What about the boolean value they return?

EDIT3: for $var I mean any variable with any value, or even an undefined variable.

Conclusion from users answers: if(!$var) and empty($var) are equivalent as described here http://php.net/manual/en/types.comparisons.php, they will return the same bool value on the same variable.

if(!$var) will return a php notice if $var is not defined, normally this is not the case (if we write good code) most IDEs will underline it.

  • When checking simple variables if(!$var) should be ok
  • When checking arrays index ($var['key']) or object properties ($var->key) empty($var['key']) is better using empty.
like image 405
albanx Avatar asked Nov 27 '14 09:11

albanx


People also ask

How do I check if a given variable is empty in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

Is 0 considered empty PHP?

From manual: Returns FALSE if var has a non-empty and non-zero value. The following things are considered to be empty: "" (an empty string) 0 (0 as an integer)

Is NULL equal to empty string PHP?

is_null() The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .

How do you check a string is empty in PHP?

We can use empty() function to check whether a string is empty or not. The function is used to check whether the string is empty or not. It will return true if the string is empty. Parameter: Variable to check whether it is empty or not.


2 Answers

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.

like image 102
James Avatar answered Sep 28 '22 13:09

James


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.

like image 31
Subodh Ghulaxe Avatar answered Sep 28 '22 14:09

Subodh Ghulaxe