Using if(!empty) with multiple variables not in an array

Using if(!empty) with multiple variables not in an array

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

Stefan Bogdanescu

Founder & Senior Architect

Why if(!empty()) Cannot Handle Multiple Scalar Variables

You might be tempted to pass a list of variables directly into a conditional check, hoping for a shortcut that skips individual validation. If you look at how if(!empty($vFoo)) behaves for a single scalar value, the logic seems straightforward: it returns false if the variable is empty (null, 0, "", etc.) and true otherwise. It's natural to assume that simply adding more variables to the parentheses—like if(!empty($vFoo, $vSomeValue, $another))—would extend this behavior to handle multiple items simultaneously. In PHP, however, function arguments are not treated as a set or collection unless you explicitly provide one.

The issue isn't with the logic of checking for emptiness; it's with how PHP interprets the syntax. When you write if(!empty($vFoo,$vSomeValue,$another)), the parser sees this as an attempt to pass three arguments to the empty() function. Since empty() is defined to accept only one argument, PHP throws a fatal error or a warning depending on your error reporting level. It does not interpret the comma-separated list as "check all of these and return true if any are empty" or "return true if all are non-empty."

To achieve the goal of rendering items only when they have values, you must explicitly define the relationship between those variables. The most direct approach is to combine them into an array first. Once the variables are grouped, empty() can evaluate the entire collection at once.

$vFoo       = $item[1]; 
$vSomeValue = $item[2]; 
$vAnother   = $item[3];

// Combine into a single array
$values = [$vFoo, $vSomeValue, $vAnother];

if (!empty($values)) {
    // Now you can iterate through the array safely
    foreach ($values as $value) {
        if (!empty($value)) {
            $result .= "<li>$value</li>";
        }
    }
}

This approach shifts the burden of filtering from the outer if statement to the inner loop. The outer condition !empty($values) ensures that you only enter the block if at least one variable holds a value. Inside, the nested check !empty($value) handles individual scalars correctly. This pattern is robust and avoids the complexity of trying to chain conditions manually.

Alternatively, if your specific requirement is to print the HTML list items only when all variables contain data, you can use logical operators to combine the checks:

if (!empty($vFoo) && !empty($vSomeValue) && !empty($vAnother)) {
    $result .= "<li>$vFoo</li>";
    $result .= "<li>$vSomeValue</li>";
    $result .= "<li>$vAnother</li>";
}

While this works, it quickly becomes unmanageable as the number of variables grows. Maintaining a chain of && operators introduces code bloat and makes future maintenance difficult. If you need to add or remove a variable from the check, you risk introducing syntax errors or logic bugs. The array-based solution scales linearly with complexity, whereas the logical operator approach scales poorly.

Another common pitfall involves how empty() treats specific values. Remember that empty() returns true for integers equal to zero. If your data includes legitimate numeric zeros (e.g., a quantity of 0 items), if(!empty($var)) will treat them as falsy and skip the output. This behavior is intentional but often surprising. If you need to distinguish between "no data" and "zero value," you should use strict comparisons like isset() combined with a type check, or simply avoid empty() in those contexts.

In modern PHP development, especially within frameworks like Laravel, arrays are the standard container for multiple related values. By consolidating scalar variables into an array before validation, you leverage built-in functions that handle iteration and filtering efficiently. This pattern reduces cognitive load and keeps your codebase clean. If you ever find yourself writing complex logic to conditionally render content based on multiple unrelated variables, consider restructuring the data model or passing them as a collection from the start.

Our network