114

Could you help me to improve my coding style?:) In some tasks I need to check - is variable empty or contains something. To solve this task, I usually do the following.

Check - is this variable set or not? If it's set - I check - it's empty or not?

<?php
    $var = '23';
    if (isset($var)&&!empty($var)){
        echo 'not empty';
    }else{
        echo 'is not set or empty';
    }
?>

And I have a question - should I use isset() before empty() - is it necessary? TIA!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Dmitry Belaventsev
  • 6,347
  • 12
  • 52
  • 75
  • empty is !isset($var) || $var == false – zloctb Oct 19 '15 at 06:37
  • 3
    in my oppinion empty() is a horrific development of PHP. As it consideres "0" in any forms as "empty" you may fall into bad traps costing lots of debugging time. I'd say: avoid at all cost and just write the logic yourself – John Nov 04 '16 at 03:45

6 Answers6

158

It depends what you are looking for, if you are just looking to see if it is empty just use empty as it checks whether it is set as well, if you want to know whether something is set or not use isset.

Empty checks if the variable is set and if it is it checks it for null, "", 0, etc

Isset just checks if is it set, it could be anything not null

With empty, the following things are considered empty:

  • "" (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 $var; (a variable declared, but without a value in a class)

From http://php.net/manual/en/function.empty.php


As mentioned in the comments the lack of warning is also important with empty()

PHP Manual says

empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

Regarding isset

PHP Manual says

isset() will return FALSE if testing a variable that has been set to NULL


Your code would be fine as:

<?php
    $var = '23';
    if (!empty($var)){
        echo 'not empty';
    }else{
        echo 'is not set or empty';
    }
?>

For example:

$var = "";

if(empty($var)) // true because "" is considered empty
 {...}
if(isset($var)) //true because var is set 
 {...}

if(empty($otherVar)) //true because $otherVar is null
 {...}
if(isset($otherVar)) //false because $otherVar is not set 
 {...}
Stefano Saitta
  • 1,844
  • 1
  • 19
  • 34
Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
  • It's mean - in the code above I check is variable set or not twice?:) – Dmitry Belaventsev Aug 25 '11 at 13:58
  • Just check whether it is empty, php won't throw an error if this is not the case. – Pez Cuckow Aug 25 '11 at 13:59
  • 3
    You are omitting the biggest point of `empty`: It doesn't throw a warning when the tested variable does not exist. That's the whole point of this function, otherwise it's identical to `== false`. – deceze Aug 25 '11 at 14:02
  • 1
    So, the fact in manual that "no warning is generated when the variable is not set" confused me. No warning doesn't mean that I will not have troubles in the code below. Now it's clear for me. Thx alot! – Dmitry Belaventsev Aug 25 '11 at 14:09
  • *"isset just checks if is it set, it could still be null"* That's not true: `isset` on a null (and existing) variable will evaluate to `false`. – netcoder Aug 25 '11 at 14:30
14

In your particular case: if ($var).

You need to use isset if you don't know whether the variable exists or not. Since you declared it on the very first line though, you know it exists, hence you don't need to, nay, should not use isset.

The same goes for empty, only that empty also combines a check for the truthiness of the value. empty is equivalent to !isset($var) || !$var and !empty is equivalent to isset($var) && $var, or isset($var) && $var == true.

If you only want to test a variable that should exist for truthiness, if ($var) is perfectly adequate and to the point.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • This one explains much much better. If we get comparison for all other cases like this `!empty is equivalent to isset($var) && $var`, it would be awesome. Thanks @deceze – G_real Jan 15 '20 at 22:15
7

You can just use empty() - as seen in the documentation, it will return false if the variable has no value.

An example on that same page:

<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>

You can use isset if you just want to know if it is not NULL. Otherwise it seems empty() is just fine to use alone.

Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
chris
  • 2,404
  • 3
  • 27
  • 33
  • Explanation for the down vote? He's not asking if they are the same, he's asking if he needs to check isset before checking empty. – chris Aug 25 '11 at 14:01
  • I didn't downvote, but "use isset if you want to know it is not null" is not correct: `$var = null; isset( $var ) == true`. – JJJ Aug 25 '11 at 14:05
  • From the PHP5/4 Manual: isset() - "Determine if a variable is set and is not NULL." http://us.php.net/manual/en/function.isset.php – chris Aug 25 '11 at 14:07
2

Here are the outputs of isset() and empty() for the 4 possibilities: undeclared, null, false and true.

$a=null;
$b=false;
$c=true;

var_dump(array(isset($z1),isset($a),isset($b),isset($c)),true); //$z1 previously undeclared
var_dump(array(empty($z2),empty($a),empty($b),empty($c)),true); //$z2 previously undeclared

//array(4) { [0]=> bool(false) [1]=> bool(false) [2]=> bool(true) [3]=> bool(true) } 
//array(4) { [0]=> bool(true) [1]=> bool(true) [2]=> bool(true) [3]=> bool(false) } 

You'll notice that all the 'isset' results are opposite of the 'empty' results except for case $b=false. All the values (except null which isn't a value but a non-value) that evaluate to false will return true when tested for by isset and false when tested by 'empty'.

So use isset() when you're concerned about the existence of a variable. And use empty when you're testing for true or false. If the actual type of emptiness matters, use is_null and ===0, ===false, ===''.

dnagirl
  • 20,196
  • 13
  • 80
  • 123
1

Empty returns true if the var is not set. But isset returns true even if the var is not empty.

Whiskas
  • 352
  • 1
  • 2
-4
$var = 'abcdef';
if(isset($var))
{
   if (strlen($var) > 0);
   {
      //do something, string length greater than zero
   }
   else
   {
     //do something else, string length 0 or less
   }
}

This is a simple example. Hope it helps.

edit: added isset in the event a variable isn't defined like above, it would cause an error, checking to see if its first set at the least will help remove some headache down the road.

chris
  • 36,115
  • 52
  • 143
  • 252
Akos
  • 1,997
  • 6
  • 27
  • 40