HOWTO · PHP
isset() vs. empty() Functions in PHP
Compare isset() and empty() in PHP, see how they treat common values, and use them to check submitted form fields.
On this page
PHP’s isset() returns true when a variable exists and is not null. empty() returns true when a value is missing or PHP treats it as false. This difference matters when checking form fields: a field can be present while containing a value that empty() considers empty.
How isset() and empty() Differ
Use isset() to check whether a variable is available and non-null. Use empty() to check whether a value is missing or falsey. In particular, 0 and the string "0" are set values, but empty() considers both empty.
| Value | isset($value) |
empty($value) |
|---|---|---|
| Undefined variable | false |
true |
null |
false |
true |
false |
true |
true |
0 |
true |
true |
"0" |
true |
true |
"" |
true |
true |
[] |
true |
true |
"hello" |
true |
false |
Other values PHP treats as false include 0.0. The key distinction is that isset() only rejects an undefined or null variable, while empty() also treats false, zero, the string "0", an empty string, and an empty array as empty. These checks describe presence and truthiness; they do not establish that input is safe or valid for a particular purpose.
Check Whether a Variable Is Set with isset()
isset() reports whether a variable is available with a non-null value. For example, a variable containing a name or the number 0 passes this check, while a variable assigned null does not. This makes the function useful before reading optional request data or using a value that may not have been assigned. It can also check several variables at once; the result is true only if every argument is defined and non-null. The following small program uses a supplied name and prints a message when PHP can read it.
Run this example:
<?php
$name = "Mina";
if (isset($name)) {
echo "Name is set.\n";
}
Name is set.
For example, isset($first, $last) checks both variables in one call. Use separate checks if you need to report which field is missing. When checking an array key such as $_GET['name'], isset() is also useful because it safely returns false if the key is absent.
Check Whether a Value Is Empty with empty()
empty() returns true for an undefined variable without producing an undefined-variable warning. It also returns true for null, false, 0, 0.0, "0", "", and an empty array. A nonempty string such as "hello" returns false. This behavior is broader than checking whether a string has zero characters, so do not use empty() when "0" should count as meaningful input.
For a value that must be present and meaningful, check both ideas deliberately. For example, isset($value) && $value !== '' rejects a missing or null variable and an empty string, but it accepts 0 and "0". Choose the condition that matches the input’s meaning rather than assuming every falsey value is invalid.
Validate Form Input with isset() and empty()
A form’s name and email fields arrive as request parameters. First check that both parameters were supplied, then confirm they are strings before trimming the name and checking whether it is blank. A query such as name[]=Mina supplies an array, so the handler reports that the fields must be text instead of passing that value to trim(). The following PHP example reads a GET submission and reports whether the name needs attention. It does not validate email syntax, ownership, or deliverability, and checking a nonblank name does not prove it contains only letters.
This minimal browser form sends both named fields as GET parameters to the page that contains it:
<form method="get">
<label>Name: <input name="name" type="text"></label>
<label>Email: <input name="email" type="text"></label>
<button type="submit">Check fields</button>
</form>
The PHP handler can then distinguish a request with both fields from a request where one was omitted:
<?php
if (!isset($_GET['name'], $_GET['email'])) {
echo "Submit both fields first.\n";
} elseif (!is_string($_GET['name']) || !is_string($_GET['email'])) {
echo "Name and email must be text fields.\n";
} else {
$name = trim($_GET['name']);
$email = $_GET['email'];
if (empty($name)) {
echo "Enter a name.\n";
} else {
echo "Name is present; email was supplied.\n";
}
}
Because empty() also treats "0" as empty, this example regards a name consisting of that character as blank. The email variable is only checked for presence; use appropriate validation and output escaping for the needs of a real application. Neither isset() nor empty() checks email format or proves that a name contains alphabetic characters.