What's the Difference Between 'Isset()' and '!Empty()' in PHP

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

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.

PHP: is there a difference between (isset($a) && !empty($a)) and $a??false

Firstly, let's understand what the different elements are doing:

  • isset takes a variable, array element, etc, and returns true if it exists, and has any value other than null
  • empty takes a variable, array element, etc, and returns false if it exists, and has any value which would be considered equivalent to true
  • ?? examines a variable, array element, etc, and returns its value if it exists, and has any value other than null; otherwise, it returns its right-hand argument
  • any expression used in an if statement is cast to boolean automatically

So, we can look at the equivalences:

  • empty is a combination of isset() and a cast to false; specifically, empty($a) === !isset($a) || !(bool)$a, so !empty($a) === isset($a) && (bool)$a
  • ?? uses the same check as isset, not the same check as empty; so $a ?? false on its own is equivalent to isset($a) ? $a : false
  • but, in an if statement, the whole thing will be forced to boolean, so if ( $a ?? false ) ... is equivalent to if ( (bool)(isset($a) ? $a : false) ) ... or more readably if ( isset($a) ? (bool)$a : false ) ...

Which brings us to the answer, which is that the following all do the same thing:

if ( isset($a) && !empty($a) ) ...
if ( isset($a) && !(bool)$a ) ... // empty() is just a bool cast + isset
if ( !empty($a) ) ... // the isset is actually redundant
if ( isset($a) ? (bool)$a : false ) ... // same logic as the && version
if ( (bool)($a ?? false) ) ... // ?? does the isset for us
if ( $a ?? false ) ... // if does the (bool) for us

Probably the most readable is just to use !empty($a) on its own, and leave ?? for where you actually need the original value rather than a true.

In where shall I use isset() and !empty()

isset vs. !empty

FTA:

"isset() checks if a variable has a
value including (False, 0 or empty
string), but not NULL. Returns TRUE
if var exists; FALSE otherwise.

On the other hand the empty() function
checks if the variable has an empty
value empty string, 0, NULL or
False. Returns FALSE if var has a
non-empty and non-zero value."

PHP: Variable empty or not set or what?

if (isset($data)) {  

Variable is just set - before that line we declared new variable with name 'data', i.e.
$data = 'abc';

if (!empty($data)) {  

Variable is filled with data. It cannot have empty array because then $data has array type but still has no data, i.e. $data = array(1);
Cannot be null, empty string, empty array, empty object, 0, etc.

if ($data != '') {  

Variable is not an empty string. But also cannot be empty value (examples above).

If we want to compare types, use !== or ===.

if ($data) {  

Variable is filled out with any data. Same thing as !empty($data).



Related Topics



Leave a reply



Submit