check if variable empty

check if variable empty

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

Stefan Bogdanescu

Founder & Senior Architect

Evaluating NULL Checks and Variable Assignment in PHP

The provided code snippet demonstrates a common pattern for handling missing or unset variables, but it relies on a logic that can be both verbose and semantically ambiguous. Specifically, the condition if ($user_id == NULL || ...) mixes loose equality with type-specific checks. In modern PHP development, relying on == for type juggling is generally discouraged because it can lead to unexpected behavior when comparing strings like '0' or empty arrays against NULL. A more robust approach is to use === for strict comparison or, even better, the dedicated is_null() function.

Strict Type Checking vs. Loose Comparison

The second question in your source material asks if these variables should be tested with is_null(). The answer is a definitive yes. Using is_null() is the standard idiom for checking the absence of a value. It avoids the pitfalls of loose comparison operators. For instance, if $user_id was somehow set to the string '0', the expression $user_id == NULL would evaluate to true, assigning -1 to it. This silently corrupts your data type. By switching to is_null($user_id), you ensure that only actual NULL values trigger the block, preserving data integrity.

Here is how the logic refines with strict typing:

if (is_null($user_id) || is_null($user_name) || is_null($user_logged)) {
    $user_id = '-1';
    // Note: Keeping user_name and user_logged as NULL is correct here.
    // However, assigning a string to an ID suggests a schema mismatch.
}

Refactoring with Arrays for Reusability

Regarding your third question about writing the checks in one line using an array: yes, this is possible, but it requires careful consideration of how you are handling the values inside the loop. If you simply iterate over an array of variables, you must ensure that the assignment logic applies correctly to each specific variable type.

A common pattern involves creating a list of variables to check and assigning a default value if any fail the null check. However, be wary of reassigning all variables inside a single loop if only one needs changing. In your original code, $user_id gets set to '-1', while $user_name and $user_logged remain NULL. A naive array loop might overwrite the valid names with '-1' if not guarded properly.

You can achieve this concisely using functional programming concepts available in PHP:

$variables = [
    'id' => $user_id,
    'name' => $user_name,
    'logged' => $user_logged,
];

// Check if any of the values are null
if (count(array_filter($variables, fn($val) => is_null($val))) > 0) {
    // Only modify the specific variables that need a default value
    if (is_null($user_id)) $user_id = '-1';
    
    // Leave others as NULL or handle them individually if needed
}

Alternatively, if your intention is to treat all three as optional fields where missing data should result in a specific state, an explicit list of checks remains the most readable and maintainable option. Over-engineering with arrays often introduces bugs related to variable scope and side effects.

The isset() Alternative

Before diving into complex array manipulations, consider whether you are actually checking for NULL or for existence. If a variable is simply unset, is_null() and isset() behave differently depending on how it was initialized. If the variables are defined but hold NULL, is_null() is the correct tool. If they might be entirely missing from the scope, isset() combined with a default value assignment using the null coalescing operator (??) is often cleaner:

$user_id = $user_id ?? '-1';
$user_name = $user_name ?? NULL; // Explicitly keeping NULL if desired
$user_logged = $user_logged ?? NULL;

This approach eliminates the need for a bulky if block and makes the intent immediately clear: "If this isn't set or is null, use this fallback."

Conclusion on Best Practices

While there is no single "shortest" way that sacrifices clarity, combining is_null() with the null coalescing operator (??) offers the best balance of brevity and safety. It avoids the type-juggling traps of loose comparison and prevents the accidental overwriting of unrelated variables in loop-based refactoring. Always prioritize explicit checks when dealing with data integrity, especially when assigning default string values to what might conceptually be integer IDs.

Our network