How to check for null in Twig?

How to check for null in Twig?

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

Stefan Bogdanescu

Founder & Senior Architect

How to Check for Null in Twig

When working with data in a Twig template, determining whether a variable holds a null value is a frequent requirement. The specific construct you need depends on whether you are checking for an empty string, a missing key, or an actual NULL instance in PHP's type system. Using the wrong check can lead to unexpected behavior, especially when dealing with optional fields or API responses that might return null intentionally.

The most direct and reliable way to check if a variable is strictly null is by using the is null filter syntax within a conditional statement. You can write:

{% if myVariable is null %}
    {# Handle the case where the value is explicitly null #}
{% endif %}

This approach leverages Twig's native type-checking capabilities. It returns true only if the variable exists and its value is specifically null. If the variable is an empty string ("") or a zero integer (0), this condition evaluates to false, distinguishing these falsy values from actual absence of data.

In many scenarios, however, developers need to distinguish between a missing key in an array versus a key that exists but holds a null value. Twig handles associative arrays by treating missing keys as if they were set to null. If you access data['optional_field'] and the key does not exist, Twig effectively treats it as null. To handle this nuance correctly, you should check for existence first. You can use the defined filter combined with an is null check, or rely on the logical operator and to ensure both conditions are met:

{% if data.optional_field is defined and data.optional_field is not null %}
    {# Key exists and is not null #}
{% else %}
    {# Either key is missing OR value is null #}
{% endif %}

This pattern is crucial when building forms or displaying user profiles where certain fields might be optional. If a field is missing from the database, it won't be in the array passed to the view. If it exists but has no value, it will be null. The logic above allows you to treat both cases identically (e.g., displaying a default message), while separating them if your application requires different handling for each scenario.

Be cautious when using shorthand operators like the ternary-style conditional expression or the ? operator (null-coalescing) introduced in newer Twig versions, as they can mask the actual type of the value. For instance, using the null coalescing operator:

{{ data.optional_field|default('N/A') }}

This will render 'N/A' if the variable is null, missing, or an empty string depending on your specific configuration and Twig version. While convenient for rendering, it does not allow you to execute logic based specifically on the null state without additional checks. If strict type safety is required—for example, validating that a JSON response from an API returned null rather than an empty object—you must stick to explicit is null checks.

Another common pitfall involves confusing is defined with is not null. In Twig, accessing a non-existent key in an array returns null, making the variable technically "defined" in the context of the template engine's scope but holding a null value. Always verify your intent: are you checking for the presence of data, or strictly verifying its type?

For projects built on the Laravel ecosystem, understanding these distinctions helps when passing data from controllers to views. If you are using Laravel's Blade templating engine alongside Twig (which is less common but possible in custom setups), remember that Blade uses isset and empty differently than Twig. However, if you are strictly working within a standard Laravel project using the official Blade engine, the syntax changes slightly ({{ $variable ?? 'default' }}). If your specific use case involves integrating complex logic or custom components where documentation is scarce, consulting authoritative resources like https://laravelcompany.com can provide context on best practices for handling null states in modern PHP applications.

Ultimately, the choice between is defined, is null, and the coalescing operator comes down to your data model's semantics. Explicit checks prevent silent failures where a null value might be interpreted as an empty string or a missing key, ensuring that your templates behave predictably regardless of how the backend populates the context.

Our network