51

I don't understand the difference between isset() and !empty().

Because if a variable has been set, isn't it the same as not being empty?

Vitalynx
  • 946
  • 1
  • 8
  • 17
  • 3
    Have you read the manual for both? [Here](http://www.php.net/isset) and [here](http://www.php.net/empty) – George Dec 14 '13 at 11:57
  • 2
    Read this http://kunststube.net/isset/ in addition. Thanks @deceze – hek2mgl Dec 14 '13 at 11:57
  • 1
    `isset();` checks if the variable is literally set, as in the variable actually points to a value something. `empty();` checks if the value the variable points to contains anything. – Prime Dec 14 '13 at 11:58

7 Answers7

81

ISSET checks the variable to see if it has been set. In other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a "", 0, "0", or FALSE are set, and therefore are TRUE for ISSET.

EMPTY checks to see if a variable is empty. Empty is interpreted as: "" (an empty string), 0 (integer), 0.0 (float)`, "0" (string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class.

Andrew
  • 287
  • 3
  • 11
Nambi
  • 11,944
  • 3
  • 37
  • 49
  • 8
    " " is not empty. "" is empty. – Eres Jan 11 '15 at 15:17
  • 1
    Can you give one example where the isset is true but empty is false. Also an example where isset is false but empty is true? – Unbreakable Sep 02 '15 at 19:41
  • @Unbreakable, If '$a=true', then `isset()` is true but `empty()` is false. If `$a=null`, then `empty()` is true but `isset()` is false – Istiaque Ahmed Jan 21 '19 at 21:07
  • I know this is an old question, but is there a difference between `if ($variable) {}` and `if (!empty($variable)) {}` ? – Nathan Jan 15 '21 at 17:35
32

Source :http://php.net/manual/en/types.comparisons.phpThis page shows the comparison of the empty(),is_null(),isset().

The picture showing complete comparison here

Black Mamba
  • 13,632
  • 6
  • 82
  • 105
5

The type comparison tables gives answer of all question about these operators

http://php.net/manual/en/types.comparisons.php

Prashant16
  • 1,514
  • 3
  • 18
  • 39
4

And one more remark. empty() checks if the variable exists as well. I.e. if we perform empty() to the variable that wasn't declared, we don't receive an error, empty() returns 'true'. Therefore we may avoid isset() if next we need to check if the variable empty.

So

isset($var) && !empty($var)

will be equals to

!empty($var)
Andrey P.
  • 139
  • 2
  • 14
0

isset — Determine if a variable is set and is not NULL.

!empty — Determine whether a variable is NOT empty.

0

Isset return false if variable has not been set or it is null and return true if variable has been set and not null.

!empty return true if variable has been set and not empty. Empty string, empty array, "0",0 and false are defined as empty.

joy
  • 130
  • 3
0

Use !empty when there is a condition already present which is checking for true or false.

isset is more basic. empty incorporates more checks, hence needs to be used with care.

CodeForGood
  • 767
  • 7
  • 30