Check if Variable exists and === true

Check if Variable exists and === true

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

Stefan Bogdanescu

Founder & Senior Architect

Check if Variable Exists and Is Strictly True

In PHP, verifying that a configuration flag or array key represents an active state is a common pattern. The requirement often breaks down into two distinct conditions: the key must exist within the scope, and its value must be strictly true. While it might seem efficient to combine these checks into a single conditional statement, doing so without care triggers specific behaviors regarding notices and strict type coercion.

When you encounter an array key that might not be set, accessing it directly—such as $config['feature'] === true—invites PHP to emit a warning or notice depending on your error reporting settings. If the key is missing, the expression evaluates to false, but the act of accessing an undefined index can generate an "Undefined array key" notice. This noise pollutes logs and debug output, making it difficult to distinguish between application logic errors and expected null states.

The standard approach to avoid these notices is a two-step verification. First, you check if the key exists using isset(). If that condition passes, you then verify the strict equality of the value. This pattern looks like this:

if (isset($config['feature']) && $config['feature'] === true) {
    // Feature is enabled and explicitly set to true
}

This method is robust because it separates concerns. The isset() call handles the existence check safely without triggering notices, even if the key is undefined or holds a value of null. Only if the key exists does the code proceed to evaluate the strict equality operator (===). Using === here is crucial because loose equality (==) would convert null, 0, or an empty string to false, which might not be the intended behavior for a boolean flag.

However, some developers attempt to condense this into a single line using the null coalescing operator or shorthand syntax. Consider the following alternative:

if (($config['feature'] ?? false) === true) {
    // Feature is enabled
}

This approach uses the null coalescing operator (??) to provide a default value of false if the key is missing. This eliminates the notice entirely and allows for a single conditional check. From a performance standpoint, this is slightly cleaner as it involves fewer function calls than chaining two conditions with &&. The trade-off is purely stylistic; both approaches are logically equivalent in terms of output, but the null coalescing version feels more concise to those familiar with modern PHP 7+ syntax.

There is a specific pitfall to watch out for regarding null versus false. If your business logic treats a missing key as "off" and an explicit false value also as "off", this pattern holds up well. However, if you are using older PHP versions or environments where loose equality was the norm, switching to strict comparison requires ensuring that the default fallback doesn't introduce unexpected type shifts. In modern Laravel projects, for instance, configuration arrays often rely on default values defined in the config package rather than relying solely on runtime array checks.

Ultimately, whether you choose the traditional isset() chain or the modern null coalescing operator depends on your team's style guide and PHP version. Both methods prevent notices and ensure that you are only executing logic when the feature is explicitly enabled. If you are managing complex configuration structures where defaults vary by environment, consider leveraging Laravel's built-in config system to handle fallbacks automatically rather than manually checking every array index.

Our network