isset(), empty() and unset() are PHP language constructs with different jobs. isset() asks whether a variable exists and is not null. empty() asks whether a value is missing or false-like. unset() removes a variable binding or an array entry. Confusing them can erase a valid zero, misinterpret an explicit null, or delete data when you only wanted to inspect it.
This guide compares their behavior using a small settings array. You will learn when array_key_exists() is the more precise check, why field validation should not be reduced to empty(), and what happens to numeric keys when you remove an array item.
isset checks existence and non-null together
isset($value) is false when the variable is undefined or its value is null. It is true for zero, false, an empty string and an empty array because those values still exist and are not null. This makes it useful for optional data when null should be treated the same as absence.
For arrays, isset($settings['timeout']) also returns false when the key exists but holds null. If you need to distinguish an absent key from an explicit null, use array_key_exists('timeout', $settings). That distinction matters in update APIs where absence can mean leave unchanged and null can mean clear the value.
You can pass several variables to isset(). It returns true only when all are set and non-null. Do not use that as a substitute for validating their values. A supplied username can be an empty string and still pass isset. A supplied count can be negative and still pass. Existence and validity are separate questions.
empty uses PHP's boolean idea of emptiness
empty($value) is true for an undefined value and for values that convert to false, including null, false, zero, an empty string, the string '0' and an empty array. It does not produce an undefined-variable warning merely because the inspected variable is missing. That convenience is useful when all those states genuinely mean no useful value.
The string '0' surprises many form handlers. If a field allows a zero rating or zero retries, empty() can reject valid input. Instead, check the field's shape, then compare the specific condition you mean. For a required nonblank string, first verify it is a string and then test trim($value) === ''.
Whitespace is another distinction. A string containing spaces is not empty under PHP's boolean rules. If your form considers whitespace-only text blank, trimming or a dedicated validation rule is necessary. Avoid describing empty as a universal blank-field check; its behavior is defined by language truthiness rather than your form's requirements.
unset removes a binding or entry
unset($name) removes that variable in the current scope. It does not assign null and does not return a replacement value for use in an expression. unset($array['key']) removes one entry. Calling unset for a missing variable or key is safe, which is useful when cleanup is intentionally idempotent.
Removing a numeric array key does not automatically renumber the remaining keys. If you remove index one from an array with keys zero, one and two, the remaining keys are zero and two. Use array_values() when you deliberately want a fresh zero-based list afterward. This is especially important before JSON encoding, where a nonconsecutive numeric array can become a JSON object rather than a JSON array.
Unsetting one variable that refers to an object does not necessarily destroy the object. Another variable may still refer to it. Likewise, reference relationships affect which binding is removed and which value survives. Treat unset as removing a name or entry, not as a command to securely erase every copy of data from memory.
Choose a check from the data contract
For optional text where null means absent, null coalescing can be clearer than a separate isset branch. For a partial update, key existence may be required to distinguish missing from explicit null. For a checkbox, validate the accepted submitted value rather than relying on every truthy string to mean checked.
Objects can define magic methods such as __isset(), so checking inaccessible properties may involve class-specific behavior. Do not assume an object check behaves exactly like an array check. When you own the class, explicit methods such as hasEmail() can express the domain rule more clearly than probing a magic property.
The PHP isset reference, empty reference and unset reference explain the language rules. Use those rules as building blocks for validation rather than asking one construct to represent every possible input policy.
PHP isset(), empty() and unset() Examples
Each example shows the code, the output it produces and what each part does.
Compare presence with null
The retry setting is a real value even though it is zero. The timeout key exists, but isset does not count a null value as set. The key-existence function answers only the structural question, which is why its answer differs from isset for that entry.
<?php
$settings = ['retries' => 0, 'timeout' => null];
var_export(isset($settings['retries']));
echo PHP_EOL;
var_export(isset($settings['timeout']));
echo PHP_EOL;
var_export(array_key_exists('timeout', $settings));
echo PHP_EOL;true
false
trueZero is present but empty
All three answers are correct because the questions differ. The value exists, it is false-like under PHP's rules, and it is not a blank string after trimming. Decide which question your business rule asks before selecting a construct.
<?php
$value = '0';
var_export(isset($value));
echo PHP_EOL;
var_export(empty($value));
echo PHP_EOL;
var_export(trim($value) === '');
echo PHP_EOL;true
true
falseCommon PHP isset(), empty() and unset() Mistakes
Rejecting valid zero or confusing removal with null
Do not write if (empty($quantity)) when a quantity of zero is a valid input. Use an explicit range rule after validating the integer. Do not assign null when your update protocol expects a removed key, and do not remove a key when explicit null has a distinct meaning.
When debugging, inspect both the keys and values rather than only printing a truthy or false result. An absent key and a null key can look identical through isset while producing different behavior elsewhere. A short check should not hide the information the application needs.
PHP isset(), empty() and unset() in Real Projects
Practical task: remove a completed queue item
You have a three-item in-memory queue. Remove the middle item, then rebuild a zero-based list before encoding it as JSON. The task demonstrates that deletion and list normalization are separate operations. It does not persist the queue or handle concurrent workers; those concerns belong to a real queue service.
<?php
$queue = ['draft', 'review', 'publish'];
unset($queue[1]);
echo implode(',', array_keys($queue)) . PHP_EOL;
$queue = array_values($queue);
echo json_encode($queue, JSON_THROW_ON_ERROR) . PHP_EOL;0,2
["draft","publish"]The key output proves that unset left a gap. array_values() then returns the remaining values with consecutive integer keys. Try encoding before normalization and compare the JSON shape. Whether you should normalize depends on the domain: an associative map with meaningful numeric identifiers should keep its keys instead of being converted into a list.
PHP isset(), empty() and unset() FAQ
Does isset return true for false?
Yes. False exists and is not null. Its boolean value is unrelated to whether isset considers it set.
Is a whitespace string empty?
Not under empty's rules. Trim and compare when the domain treats whitespace as blank.
Can unset return the deleted value?
No. Read and store the value first if you need it, then remove the entry deliberately.
Which check distinguishes missing from null?
For an array key, array_key_exists distinguishes those states. Combine it with value validation when the update requires more than presence.
Should I always call array_values after deleting?
No. Do it only when the result should be a list. Preserve keys that carry identity or other meaning.
PHP isset(), empty() and unset() Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
After removing index one from a list, which function deliberately restores zero-based consecutive keys?
PHP isset(), empty() and unset() Quiz
2 questions to check what you learned.
What is isset($x) when $x is null?
- false
- true
- It always deletes $x
Which check can detect an existing array key whose value is null?
- isset() alone
- array_key_exists()
- unset()