25

I also get confused how to check if a variable is false/null when returned from a function.

When to use empty() and when to use isset() to check the condition ?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Harsha M V
  • 54,075
  • 125
  • 354
  • 529

7 Answers7

16

For returns from functions, you use neither isset nor empty, since those only work on variables and are simply there to test for possibly non-existing variables without triggering errors.

For function returns checking for the existence of variables is pointless, so just do:

if (!my_function()) {
    // function returned a falsey value
}

To read about this in more detail, see The Definitive Guide To PHP's isset And empty.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    i am actually trying to catch a return array or false and trying to determine further what to do – Harsha M V Jul 13 '12 at 08:18
  • 4
    OK? So `$return = myfunction(); if (!$return) { // 't was false! }` – deceze Jul 13 '12 at 08:20
  • @tkoom Would you be happier if the syntax was more operator-like? – deceze Dec 13 '13 at 19:58
  • @deceze, if `empty` is not a function, it'd be nice if it didn't pretend one. And it does and it confuses programmers, just as a lot of things does (see http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/) – ducin Dec 14 '13 at 01:20
  • 3
    Note that this also catches empty strings and empty arrays and 0 and '0', which might not be what you want – grasevski Aug 06 '15 at 00:50
11

Checking variable ( a few examples )

if(is_null($x) === true) // null
if($x === null) // null
if($x === false)
if(isset($x) === false) // variable undefined or null
if(empty($x) === true) // check if variable is empty (length of 0)
6

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.

user1289347
  • 2,397
  • 1
  • 13
  • 16
Rupesh Pawar
  • 1,887
  • 12
  • 17
2

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 (0 as an integer), 0.0 (0 as a float), "0" (0 as a string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class.

2

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

$a = "test";
$b = "anothertest";

var_dump(isset($a));      // TRUE
var_dump(isset($a, $b)); // TRUE

unset ($a);

var_dump(isset($a));     // FALSE

empty — Determine whether a variable is empty

<?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';
}
?>
Naren Karthik
  • 349
  • 2
  • 9
1

It is important to use the correct function / notation, not just whatever appears to work correctly. There are a few things to consider that are not mentioned in the existing answers.

  • Use isset to check if a variable has either not been set or has been set to null.
  • Use empty to check if a variable == false. null is cast to false and as with isset, no notice is thrown if the variable has not been set.

if (!$variable) or if ($variable == false) is the same as empty, except that a notice will be thrown if the variable has not been set.

if ($variable !== null) is the same as isset, except that a notice will be thrown if the variable has not been set.

NB

if (!$variable) and if ($variable !== null) perform better than their respective functions but not when notices are being generated, therefore, $variable needs to have been set. Don't suppress notices as a micro-optimisation, as this will make your code harder to debug and even suppressed notices cause a performance penalty.

Coalescing operators

If you are checking a variable so that you can assign a value to it, then you should use ??, ?: instead of if statements.

??

?? assigns a value when not equal to null. $variable = $a ?? $b is the same as:

if (isset($a))
    $variable = $a;
else
    $variable = $b;

?:

?: assigns a value when not == to false. $variable = $a ?: $b is the same as:

if ($a)
    $variable = $a;
else
    $variable = $b;

but remember that a notice will be generated when $a has not been set. If $a might not have been set, you can use $variable = !empty($a) ? $a : $b; instead.

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
0
  • check false: if ($v === false)
  • check null: if (is_null($v))

empty() is an evil.It is slow,and when $v queals false,0,'0',array(),'',it will return true.if you need this kind of checking,you can use if ($v).

Allen
  • 9
  • 1
  • 4
    `empty` is not evil, you simply do not understand its use case. May I direct you to [The Definitive Guide To PHP's isset And empty](http://kunststube.net/isset/)? – deceze Jul 13 '12 at 05:43