PHP 7 interview question · Most asked

What is the difference between `isset()`, `empty()` and `is_null()`?

Summary

isset() is true if a variable exists and isn't null; empty() is true if it doesn't exist or is falsy; is_null() is true only for null and warns on undefined variables.

Longer Answer

empty('0') is true, a common bug for numeric form input. isset() and empty() are language constructs that don't emit notices for undefined variables or keys. isset($arr['k']) is false when the key exists with a null value; use array_key_exists() to distinguish.

Key Points

  • isset: exists and not null
  • empty: missing or falsy ('0', 0, '', [], null, false)
  • is_null: strictly null
  • array_key_exists for null-valued keys

Comparison / Tradeoffs

Value isset empty is_null
null false true true
'' true true false
'0' true true false
[] true true false

More most-asked PHP 7 questions

← All PHP 7 questions