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 nullempty: missing or falsy ('0', 0, '', [], null, false)is_null: strictly nullarray_key_existsfor 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
- What are typed properties in PHP 7.4?
- How do return type declarations work in PHP 7?
- What were the headline improvements of PHP 7.0 over PHP 5.6?
- Explain scalar type declarations and coercive vs strict mode in PHP 7.
- Explain the `Throwable`, `Error` and `Exception` hierarchy introduced in PHP 7.
- What are nullable types in PHP 7.1?
- What does the null coalescing operator `??` do, and how is it different from `isset()` with a ternary?
- What are arrow functions in PHP 7.4 and how do they differ from closures?