What's the difference between 'isset()' and '!empty()' in PHP?

44,805

Solution 1

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.

For more information, see this article

Solution 2

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

Solution 3

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

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

Solution 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)
Share:
44,805

Related videos on Youtube

Vitalynx
Author by

Vitalynx

Updated on July 09, 2022

Comments

  • Vitalynx
    Vitalynx almost 2 years

    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?

    • George
      George over 10 years
      Have you read the manual for both? Here and here
    • hek2mgl
      hek2mgl over 10 years
      Read this kunststube.net/isset in addition. Thanks @deceze
    • Prime
      Prime over 10 years
      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.
  • hek2mgl
    hek2mgl over 10 years
    !isset(NULL) === empty(NULL).
  • Gromski
    Gromski over 10 years
    So how is "empty" defined? Insufficient explanation.
  • EresDev
    EresDev over 9 years
    " " is not empty. "" is empty.
  • Unbreakable
    Unbreakable almost 9 years
    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?
  • Istiaque Ahmed
    Istiaque Ahmed over 5 years
    @Unbreakable, If '$a=true', then isset() is true but empty() is false. If $a=null, then empty() is true but isset() is false
  • Nathan
    Nathan over 3 years
    I know this is an old question, but is there a difference between if ($variable) {} and if (!empty($variable)) {} ?