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).
Valueisset()empty()is_null()
NULLfalsetruetrue
0truetruefalse
“” (empty string)truetruefalse
“Hello”truefalsefalse
Undefined variablefalsetruetrue (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
PHP Variables
PHP Data Types
PHP Constants
PHP isset() Function
PHP empty() Function
PHP is_null() Function
Exercises & Assignments
Write a program to print “Hello PHP” using variable
Write a program to print a string using echo+variable.
Write a program to print two variables in single echo
Interview Questions & Answers
How to check if a variable is empty in PHP?
Is PHP a case sensitive language?
What are the different data types in PHP?
How to define a Constant in PHP?
What is the difference between $var and $$var in PHP?
Which special characters are allowed in PHP variable names?
Can we print multiple variables in a single echo statement in PHP?