4

Stupid question, I know, but if I don't know whether a variable $var is set or not, should I use

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

to check if it has any value in it, or is

!empty($var) enough? Would there be a problem if $var is null in the second case?

federico-t
  • 12,014
  • 19
  • 67
  • 111
  • 2
    Why not just check the php manual http://php.net/manual/en/function.empty.php – Cyclonecode Nov 30 '11 at 22:59
  • With that criteria there wouldn't be any questions in StackOverflow, right? – federico-t Nov 30 '11 at 23:04
  • 2
    Krister has a point. The manual states `empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.` You could conclude from this that it's ok to *not* use `isset()`. I'm not against your question though, as others may not make that connection. – simshaun Nov 30 '11 at 23:07
  • Oh, I didn't know it was so explicit in the manual. I thought only on certain occasions the use of empty was enough. Anyways, thanks for the answers to everybody! – federico-t Dec 01 '11 at 03:31

4 Answers4

11

isset and empty are both language constructs. And empty() internally does an isset check first, then negates that, or alternatively also checks for values that equate FALSE in boolean context.

So yes, !empty() is sufficient.

mario
  • 144,265
  • 20
  • 237
  • 291
  • Incorret, if $var is set to 0, then it IS set, but it is considered empty. – kylex Nov 30 '11 at 23:06
  • 1
    @kylex. mario is correct specifically because the op is also checking if the variable is not empty. If he were only wanting to know if the variable was set, then yes, he should use isset(). – simshaun Nov 30 '11 at 23:08
  • Let `$a=0;` then `var_dump(isset($a) && !empty($a));` and `var_dump(!empty($a));` will both say `false`. Same for undefined vars. – mario Nov 30 '11 at 23:09
  • Are we considering 0 a non-value then? It's difficult to tell by the question. – kylex Nov 30 '11 at 23:11
  • The crux is the negation here. `empty` would consider `0` falsy. But since it queries `!empty` it wouldn't matter. – mario Nov 30 '11 at 23:13
  • @mario, I think you have it backwards. empty would consider 0 TRUE, since 0 IS empty, when negated it becomes false. If `empty(0) === true` but `!empty(0) === false`. – kylex Dec 06 '11 at 22:18
  • @kylex: Well, yes. `empty` is equivalent to the boolean typecast `(bool)$var` (except that it also probes for existence in the local varscope). `0` is a *falsy* value, hence the `empty()` check returns `true`. -- OPs question was all about testing whether there is a **non-falsy** value, not if the variable is just set. And that's what `empty()` does. Not sure where you were going otherwise the `0` example. – mario Dec 06 '11 at 23:26
2

Yes, you can drop the isset():

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

You can see this with the following code:

error_reporting(E_ALL);

var_dump(empty($var));
bool(true)

(Note the lack of an undefined variable warning)

2

You should use isset(), as !empty() will return false if your $var is 0.

<?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 false because $var is empty
if(!empty($var)){

}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>
kylex
  • 14,178
  • 33
  • 114
  • 175
  • @tandu: Yes, it will. 0 is considered empty, it's stated in the manual: http://php.net/manual/en/function.empty.php – kylex Nov 30 '11 at 23:05
1

I think you can drop that isset() completely in case you're using empty(), because empty() checks whether variable is set first, too

so after all, use

if (!empty($var)) {
    //isset and not empty
}
Martin.
  • 10,494
  • 3
  • 42
  • 68