DEV Community

Cover image for Why I Use the empty() Language Construct More than isset() in PHP and You Should Too
Ramin Omrani
Ramin Omrani

Posted on

Why I Use the empty() Language Construct More than isset() in PHP and You Should Too

When developing in PHP, handling variable checks effectively is crucial to ensure robust and error-free code. Two frequently used language constructs for this purpose are empty() and isset(). However, in many cases, choosing empty() might be more advantageous than isset(), primarily because empty() covers nearly all scenarios that isset() does, along with additional checks. This article will argue why it is generally better to use empty() consistently in PHP and will demonstrate this with multiple code examples.

Understanding isset() and empty()

Before delving into specifics, let’s clarify what these language constructs do:

isset(): This language construct checks if a variable is set and is not NULL. It returns true if the variable exists and its value is not NULL; otherwise, it returns false.

$var = 0;
echo isset($var); // Outputs: 1 (true)
Enter fullscreen mode Exit fullscreen mode

empty(): This language construct determines whether a variable is considered “empty”. A variable is considered empty if it does not exist or if its value equals false. empty() returns true if the variable is empty according to the conditions as mentioned earlier; otherwise, it returns false.

$var = 0;
echo empty($var); // Outputs: 1 (true)
Enter fullscreen mode Exit fullscreen mode

Why I Like empty() More?

The primary advantage of using empty() over isset() lies in its ability to handle multiple checks simultaneously. empty() will check if a variable is set and if its value is empty ("", 0, 0.0, "0", null, false, and an empty array). This eliminates the need to use both isset() and a value check separately, reducing code complexity and potential errors.

Code Example 1: Checking Default Values

$input = $_POST['data'] ?? '';

// Using isset()
if (isset($input) && $input !== '') {
    echo "Input is set and not empty.";
} else {
    echo "Input is not set or empty.";
}

// Using empty()
if (!empty($input)) {
    echo "Input is set and not empty/falsy.";
} else {
    echo "Input is not set or empty/falsy.";
}
Enter fullscreen mode Exit fullscreen mode

In the example above, empty() simplifies the check by consolidating it into a single condition.

Code Example 2: Working with Arrays

When working with arrays, especially with keys that may or may not exist, empty() is particularly useful to avoid notices about undefined indexes.

$data = ['username' => 'JohnDoe'];

// Using isset()
if (isset($data['username']) && $data['username'] !== '') {
    echo "Username is set and not empty.";
} else {
    echo "Username is missing or empty.";
}

// Using empty()
if (!empty($data['username'])) {
    echo "Username is set and not empty/falsy.";
} else {
    echo "Username is missing or empty/falsy.";
}
Enter fullscreen mode Exit fullscreen mode

Code Example 3: Boolean Checks

When checking boolean flags, especially those that might not be set, empty() can avoid extra conditional checks.

$options = ['enabled' => false];

// Using isset()
if (isset($options['enabled']) && $options['enabled']) {
    echo "Feature is enabled.";
} else {
    echo "Feature is disabled.";
}

// Using empty()
if (!empty($options['enabled'])) {
    echo "Feature is enabled.";
} else {
    echo "Feature is disabled.";
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

In PHP, using empty() is often more efficient than isset() because it comprehensively checks whether a variable is both set and has a non-false value. This makes it an excellent tool for reducing code verbosity and complexity while ensuring the robustness of checks. While there are specific cases where isset() is necessary such as when distinguishing between false, null, and unset values. in general, empty() provides a simple and powerful alternative for most use cases.

Top comments (0)