Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if (!empty($thing)) vs if($thing)

Are these two statements executed identically, given $thing could be of any type?

if (!empty($thing)) {
    // do stuff
}

if ($thing) {
    // do stuff
}

I'm aware I could try it, but I'm not sure I'd catch all the edge cases... I'm afraid in some situations they would execute identically, but not all.

like image 546
jondavidjohn Avatar asked Nov 21 '12 16:11

jondavidjohn


1 Answers

If $thing is undefined, then if ($thing) would throw a (non-fatal) error while if (!empty($thing)) would return false.

See empty() in the PHP documentation.

like image 101
MrGlass Avatar answered Sep 27 '22 23:09

MrGlass