0
0
PhpComparisonBeginner · 3 min read

Isset vs Empty in PHP: Key Differences and Usage Guide

In PHP, isset() checks if a variable exists and is not null, while empty() checks if a variable is not set or its value is considered empty (like 0, '', or null). Use isset() to confirm a variable exists, and empty() to check if a variable has a 'non-value' or is unset.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of isset() and empty() in PHP.

Featureisset()empty()
PurposeChecks if variable exists and is not nullChecks if variable is not set or is empty
Returns true if variable isSet and not nullUnset or has empty value (0, '', null, false, [])
Returns false if variable isUnset or nullSet and has non-empty value
Variable must be defined?Yes, or returns falseNo, does not raise error if undefined
Common use caseCheck if variable exists before useCheck if variable is empty or missing
Type of checkExistence and nullityFalsy value or unset check
⚖️

Key Differences

The isset() function in PHP returns true only if the variable exists and its value is not null. It is mainly used to verify that a variable has been declared and initialized with a value other than null. If the variable is not set or is explicitly null, isset() returns false.

On the other hand, empty() checks if a variable is either not set or holds a value that PHP considers "empty". This includes 0, "" (empty string), false, null, an empty array, or the string "0". Unlike isset(), empty() does not raise an error if the variable is not defined, making it safer for checking variables that may not exist.

In summary, isset() is about existence and non-nullity, while empty() is about whether a variable is missing or holds a "falsy" or empty value.

⚖️

Code Comparison

Here is an example using isset() to check if a variable exists and is not null before printing it.

php
<?php
$var1 = 0;
$var2 = null;

if (isset($var1)) {
    echo "var1 is set and not null: $var1\n";
} else {
    echo "var1 is not set or is null\n";
}

if (isset($var2)) {
    echo "var2 is set and not null: $var2\n";
} else {
    echo "var2 is not set or is null\n";
}

if (isset($var3)) {
    echo "var3 is set and not null: $var3\n";
} else {
    echo "var3 is not set or is null\n";
}
?>
Output
var1 is set and not null: 0 var2 is not set or is null var3 is not set or is null
↔️

Empty Equivalent

Here is the same example using empty() to check if variables are empty or not.

php
<?php
$var1 = 0;
$var2 = null;

if (empty($var1)) {
    echo "var1 is empty or not set\n";
} else {
    echo "var1 has a non-empty value: $var1\n";
}

if (empty($var2)) {
    echo "var2 is empty or not set\n";
} else {
    echo "var2 has a non-empty value: $var2\n";
}

if (empty($var3)) {
    echo "var3 is empty or not set\n";
} else {
    echo "var3 has a non-empty value: $var3\n";
}
?>
Output
var1 is empty or not set var2 is empty or not set var3 is empty or not set
🎯

When to Use Which

Choose isset() when you need to confirm that a variable exists and is not null, especially before accessing it to avoid errors. It is ideal for checking if a variable has been initialized.

Choose empty() when you want to check if a variable is either unset or holds a value that PHP treats as empty or false-like, such as 0, an empty string, or false. It is useful for validating input or conditions where "empty" values should be treated as missing or invalid.

In short, use isset() for existence checks and empty() for emptiness or falsy value checks.

Key Takeaways

isset() checks if a variable exists and is not null.
empty() checks if a variable is unset or holds an empty/falsy value.
empty() does not raise errors for undefined variables, unlike isset().
Use isset() to verify variable initialization before use.
Use empty() to validate if a variable is missing or has no meaningful value.