Check if Variable is Null (isset())

You can use isset() to determine if a variable is declared and is different than null. It returns false when checking variables that has been assigned to null, this exclude the false boolean value; i.e. isset(false) will return true.

You can perform ternary operation using isset() to set value if it is null:

1
2
3
4
# Assign default value using Standard if-conditional
if (is_null($var)) { $var = 'default'; } 
# Assign default value using Short-hand ternary operation 
$var = isset($var) ? $var : 'default';

(*There’s also empty() and is_null() but they have some slightly difference , you can refer to the following table I copied from this stack-overflow post for reference:

“foo”""0“0”FALSENULL[](object) []undefined
empty()FALSETRUETRUETRUETRUETRUETRUEFALSETRUE
is_null()FALSEFALSEFALSEFALSEFALSETRUEFALSEFALSETRUE (ERROR)
isset()TRUETRUETRUETRUETRUEFALSETRUETRUEFALSE

Null Coalescing Operator/Assignment (??/??=)

(For PHP <7.4) You can use ?? to assign value to a new variable, or set fallback value if the source if null:

1
2
// Assigns 'default' if $username is null or doesn't exist.
$display_name = $username ?? 'default';

(For PHP ≥7.4) You can use ??= to assign value to variable when it is null:

1
2
3
// Assign 'default' if $username is null or doesn't exist.
$username ??= 'default';
$display_name = $username 

Null-Safe Operator (?->)

If you need to access a property or method on an object that might be null, use the null-safe operator. This prevents “Call to a member function on null” errors.

1
$country = $session?->user?->getAddress()?->country ?? 'Unknown';

Check if Variable is Null ({{var is null}})

You can use is null to check if a variable is null (note that none is just another alias for null), and you can use ternary operation (?:) to assign a default values when certain variable is null.

1
2
3
4
5
{# Assign default values, standard if-conditional #}
{% if var is null %} {% set var = "default" %} {% endif %}

{# Assign default values, using ternery operation #}
{% set var = (var is null) "default" : var %}

(*Please note that: when an object is null, it cannot be undefined, so to capture all the cases, we’ll usually use the following:

1
2
3
4
5
{# Check if object is defined and not-null (note that the sequence matters !!!)#}
{% if (var is defined) and (var is not null) %} 

{# Check if object is undefined or null #}
{% if (var is not defined) or (var is null) %}

Null Coalescing Operator (??)

You can use ?? to assign default value when a variable is null (identical to php):

1
2
3
4
5
{# directly use the variable, fallback to default value when null #}
{{ var ?? 'default'}} 

{# assign default value variable #}
{% set var = var ?? 'default' %}

Null-Safe Operator (?.)

The null-safe operator (?.) enables safe navigation through values that may be null. If the left operand is null, it returns null instead of throwing an exception.

1
2
3
4
5
{# returns null if user is null, otherwise returns user.name #}
{{ field_title?.value }}

{# null-safe operator can be chained for safe navigation #}
{{ company?.department?.team?.person }}