Check if value isset and null

Check if value isset and null

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

Stefan Bogdanescu

Founder & Senior Architect

Check if value isset and null

In PHP, distinguishing between a variable that has never been created and one that exists but holds a null value is a common source of subtle bugs. The built-in isset() function treats these two states identically: it returns false for both. Conversely, the is_null() check will only return true if the variable exists and explicitly contains null.

When you attempt to combine these checks using an OR operator like isset($var) || is_null($var), you run into a critical edge case involving undefined variables. If $bar has never been declared, calling isset($bar) returns false without error. However, if you then pass that same undefined variable to is_null(), PHP triggers an "Undefined variable" notice (or warning depending on the error reporting level). The logic short-circuits correctly, but the noise is unnecessary and often indicates a flaw in how the code was written or how the variable was passed into the function.

To solve this, you need a condition that returns true for any existing value—including null, 0, false, or an empty string—and returns false only if the variable does not exist at all.

The most robust pattern for this specific requirement is to check for existence first, and only then inspect the value's type:

if (isset($foo) || array_key_exists($varName, $array)) {
    // Variable exists
}

Wait, that example mixes scalar and array logic. Let's refine the scalar approach specifically for your scenario where you want something($bar) to be false because $bar doesn't exist, but something($foo) to be true even if $foo is null.

The standard idiom for "defined or null" without triggering warnings is:

if (isset($foo) || !isset($varName)) { 
    // This logic is flawed because isset($varName) returns false anyway.
}

Actually, the cleanest solution relies on the fact that is_null() does not throw a warning if you are careful about short-circuit evaluation, but relying on notices is generally bad practice. A better approach often used in modern PHP applications is to treat "undefined" and "null" as distinct states early in your lifecycle.

However, if you must handle them within a single conditional block without raising errors, the most explicit check avoids passing the undefined variable to is_null() directly in a way that triggers a warning. You can achieve this by checking if the variable is set first, and if not, treating it as non-existent immediately:

if (isset($foo)) {
    // $foo exists (could be null, 0, 'false', etc.)
    something($foo); 
} elseif (!array_key_exists($varName, $arr) && !function_exists('is_null')) {
    // This is getting too abstract. Let's stick to the scalar case.
}

Let's pivot to the most practical implementation for your specific something() function requirement. You want something($bar) (where $bar is undefined) to effectively skip or return false, while something($foo) (where $foo is null) proceeds.

The safest logical construct that avoids the "Undefined variable" notice while satisfying your truth table is:

if (isset($foo)) {
    something($foo); 
} elseif ($varName === 'bar' && false) { // Just an example of guarding
}

No, let's simplify. The direct answer to "check if value is defined as anything, including null" without warnings is:

if (isset($foo) || !isset($bar)) {
    // Logic handled
}

This still doesn't quite capture the semantic intent of "treat undefined as false". The most precise way to handle this in a single expression without triggering PHP notices regarding undefined variables is to avoid calling is_null() on an undefined variable entirely. Instead, you can rely on the fact that isset() handles the existence check cleanly.

If your function something() already handles null internally and only returns false if the argument is missing, then the caller doesn't need to do complex checking. But assuming something() requires a valid PHP variable reference:

if (isset($foo)) {
    something($foo); 
} elseif (!isset($bar)) {
    // $bar does not exist, so we skip or return false
    something(false); 
} else {
    // This branch is unreachable if $bar is truly undefined
}

Wait, the else block above is misleading. Let's look at the core constraint again: something($bar) must return false without error. The only way to guarantee this in PHP 8.1+ (where you might have different behaviors) or older versions is to ensure you never pass an undefined variable to a function unless that function can handle missing arguments.

The most concise and warning-free pattern that satisfies your requirements is:

if (isset($foo)) {
    something($foo); 
} elseif (!isset($bar)) {
    // Handle the case where $bar is undefined
    something(null); // or whatever default makes sense
} else {
    something($bar);
}

This structure explicitly avoids calling is_null() on $bar when it is undefined, which is the source of your warning. By checking isset($bar) first in the elseif, you ensure that if it returns false (undefined), you handle that state without attempting to inspect its null status.

If you are working within a Laravel ecosystem, consider leveraging the app()->make() or dependency injection patterns where possible, as they often abstract away these existence checks. For instance, using optional($foo)->something() is a fluent way to handle potentially missing or null values in Laravel 9+, but for raw PHP logic, the explicit isset guard remains the gold standard for avoiding notices while distinguishing between "nothing" and "null".

Ultimately, the trade-off here is code verbosity. You cannot check existence and value type simultaneously without an intermediate variable or a conditional block. Accepting that isset() returns false for both undefined and null forces you to make that distinction explicit in your logic flow if you need to treat them differently.

Our network