Why Check Both Isset() and !Empty()

Why check both isset() and !empty()

This is completely redundant. empty is more or less shorthand for !isset($foo) || !$foo, and !empty is analogous to isset($foo) && $foo. I.e. empty does the reverse thing of isset plus an additional check for the truthiness of a value.

Or in other words, empty is the same as !$foo, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.

The manual puts it like this:

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

You can simply use !empty($vars[1]) here.

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."

Is this (isset() && !empty()) function redundant?

A variable that isn't set is considered empty by empty().

Therefore, if the variable is not set, it is also empty.

Your code could be shortened by using:

echo !empty($x);

I suppose that makes the answer to your question yes.

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.

For more information, see this article

isset() and empty() - what to use

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
{...}

Is empty() enough or use isset()?

Op codes generated from isset($var) && !empty($var)

line     #  *  op                           fetch           ext  return  operands
---------------------------------------------------------------------------------


3 0 > EXT_STMT
1 ISSET_ISEMPTY_VAR 12800000 ~0 !0
2 > JMPZ_EX ~0 ~0, ->6
3 > ISSET_ISEMPTY_VAR 11800000 ~1 !0
4 BOOL_NOT ~2 ~1
5 BOOL ~0 ~2
6 > FREE ~0
4 7 > RETURN 1

and from !empty($var)

line     #  *  op                           fetch           ext  return  operands
---------------------------------------------------------------------------------
3 0 > EXT_STMT
1 ISSET_ISEMPTY_VAR 11800000 ~0 !0
2 BOOL_NOT ~1 ~0
3 FREE ~1
4 4 > RETURN 1

So yes it does repeat ISSET_ISEMPTY_VAR but !$empty. It depends on the value passed, but it it doesnt exist the top way is one opcode less BOOL_NOT. but if it does exist, the bottom way is quicker.

Though its unlikely this is going to be a bottleneck in any application

How can I use PHP's isset() in addition to empty()?

Is this what you mean?

$pagesize = (isset($_GET['pagesize']) && !empty($_GET['pagesize'])) ? 
$_GET['pagesize'] :
20;

http://us.php.net/manual/en/language.operators.logical.php

EDIT:

To be complete, empty already checks if something is set, so you don't need to use isset() as well.

I would also caution against using this code if it is going directly into a query or something similar. Consider using intval, is_numeric and similar functions.

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.

Purpose of !$ and difference b/w isset and empty?

  1. !$ means NOT thus if (!$a) and $a == TRUE then it is NOT TRUE

    Reference: http://www.php.net/manual/en/language.operators.logical.php

  2. ISSET returns TRUE if the variable exists and has a value other than NULL

    EMPTY checks to see if a variable is empty.

    Reference: http://php.net/manual/en/function.isset.php
    http://php.net/manual/en/function.empty.php



Related Topics



Leave a reply



Submit