What is the difference between isset(), empty() and is_null() in PHP?
All three check a variable, but in different ways.
- isset() is true when the variable exists and is not NULL.
- empty() is true when the variable does not exist or has a false-like value (0, “”, “0”, NULL, false, []).
- is_null() is true only when the value is NULL (and it gives a warning for an undefined variable).
| Value | isset() | empty() | is_null() |
|---|---|---|---|
| NULL | false | true | true |
| 0 | true | true | false |
| “” (empty string) | true | true | false |
| “Hello” | true | false | false |
| Undefined variable | false | true | true (with warning) |
<?php
$a = null;
$b = 0;
$c = "Hello";
var_dump(isset($a), empty($a), is_null($a));
var_dump(isset($b), empty($b), is_null($b));
var_dump(isset($c), empty($c), is_null($c));
?>
Output:
Tutorials Class - Output Window
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
Each group of three lines is for one variable: $a, $b and $c. Read the full chapters: isset(), empty() and is_null().
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |