isset() and empty() - what to use

isset() and empty() - what to use

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why You Should Skip isset() When Using empty()

In many PHP projects, developers instinctively chain isset() before empty(). The logic seems sound: first check if a variable exists, then check if it has a "truthy" value. The pattern often looks like this:

<?php
$var = '23';
if (isset($var) && !empty($var)) {
    echo 'not empty';
} else {
    echo 'is not set or empty';
}

While this code works for standard variables, it is redundant and introduces unnecessary complexity. The short answer is that you should generally drop the isset() check if you are already using empty(). Understanding exactly what empty() does reveals why the extra condition is superfluous.

The function empty() checks whether a variable is empty in a very specific sense. It returns true (meaning the variable is considered "empty") for the following cases:

  • The variable does not exist.
  • The variable is set to null.
  • The variable is an empty string ("").
  • The variable is the integer or float 0.
  • The variable is the boolean false.
  • The variable is an empty array ([]).

Notice the first bullet point: the function handles non-existent variables gracefully. If a variable has never been defined, empty() simply returns true without throwing a warning. This behavior means that wrapping it with isset() defeats its purpose.

Consider what happens if you try to check for existence separately. If you write if (isset($var)), you are explicitly asking the engine to confirm the variable is in the current scope. However, once inside the empty() function, that question is answered automatically. By combining them with &&, you are essentially saying: "Is this variable set?" AND "Is this variable not empty?". Since empty() already returns true if the variable isn't set, the first condition adds no filtering value; it only adds a function call to the execution stack.

The Hidden Pitfall of isset()

There is one specific scenario where isset() becomes critical: checking for null.

The isset() function returns false if a variable is set explicitly to null. Conversely, empty(null) returns true. This distinction is crucial when you need to differentiate between "variable doesn't exist" and "variable exists but holds no value."

However, in the common pattern of checking "is this string non-empty?", empty() covers both bases. It treats a missing variable exactly the same as an empty string or a zero value regarding its return state. Therefore, for standard validation logic, the chained approach is overkill.

When to Actually Use Both

The only time you should combine these functions is if your specific logic requires distinguishing between a null value and a non-existent key in an associative array, or if you are dealing with properties of objects where empty() might behave unexpectedly based on context. But even then, modern PHP best practices often suggest using explicit checks or type hinting instead of relying on the ambiguity of these two functions combined.

A Cleaner Approach

If your goal is simply to ensure a variable has content (like a non-empty string), simplify the condition:

<?php
// Recommended: Clean and concise
if (!empty($var)) {
    echo 'not empty';
} else {
    echo 'is not set or empty';
}

This version is more readable and performs slightly better by avoiding an unnecessary function call. If you need to handle null specifically as a valid state distinct from "missing", switch to explicit comparisons:

<?php
// Recommended for null-aware logic
if ($var !== null && $var !== '') {
    echo 'has content';
}

For more complex array validation or when working with Laravel collections, the framework often provides utility methods that abstract these checks further. If you are building a larger application and need robust form validation rules, exploring dedicated validation packages or utilizing LaravelCompany resources for best practices on handling input data can provide additional layers of safety beyond simple isset() checks.

Ultimately, the principle is to avoid doing more work than necessary. If empty() satisfies your condition, let it do the heavy lifting without the extra baggage of isset().

Our network