This is an automated archive.

The original was posted on /r/php by /u/Web-Dude on 2023-08-24 15:54:30+00:00.


So I saw this answer on Stackoverflow comparing empty() vs is_null() vs isset():

Is his example at the bottom still the best way in modern PHP?

 “foo”    “”    0 FALSE NULL undefined
empty() FALSE TRUE TRUE TRUE TRUE TRUE
is_null() FALSE FALSE FALSE FALSE TRUE TRUE  (ERROR)
isset() TRUE TRUE TRUE TRUE FALSE FALSE

If you want to check if there’s any value other than null or undefined, use isset($var)(because !is_null() generates a warning on undefined variables.)

If you want to check if the value is non-blank text or any number including zero, it gets trickier:

if (!empty($v) || (isset($v) && ($v === 0 || $v === '0'))) {
    // $v is non-blank text, true, 0 or '0'
    // $v is NOT an empty string, null, false or undefined
}