PHP Null Coalescing Operator (original) (raw)

Skip to content

Summary: in this tutorial, you’ll learn about the PHP Null coalescing operator to assign a value to a variable if the variable doesn’t exist or null.

Introduction to the PHP null coalescing operator #

When working with forms, you often need to check if a variable exists in the $_GET or $_POST by using the ternary operator in conjunction with the isset() construct. For example:

<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>i</mi><mi>s</mi><mi>s</mi><mi>e</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">name = isset(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">i</span><span class="mord mathnormal">sse</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>_POST['name']) ? $_POST['name]: '';Code language: PHP (php)

This example assigns the $_POST['name'] to the $name variable if $_POST['name'] exists and not null. Otherwise, it assigns the '' to the $name variable.

To make it more convenient, PHP 7.0 added support for a null coalescing operator that is syntactic sugar of a ternary operator and isset():

<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">name = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>_POST['name'] ?? 'John';Code language: PHP (php)

In this example, the ?? is the null coalescing operator. It accepts two operands. If the first operand is null or doesn’t exist, the null coalescing operator returns the second operand. Otherwise, it returns the first one.

In the above example, if the variable name doesn’t exist in the $_POST array or it is null, the ?? operator will assign the string 'John' to the $name variable. See the following example:

`<?php

var_dump(false ?? 'default'); // false var_dump(true ?? 'default'); // true var_dump('' ?? 'default'); // "" var_dump(0 ?? 'default'); // 0 var_dump([] ?? 'default'); // array() var_dump(null ?? 'default'); // default`Code language: PHP (php)

As you can see clearly from the output, the ?? operator is like a gate that doesn’t allow null to pass through.

Stacking the PHP Null coalescing operators #

PHP allows you to stack the null coalescing operators. For example:

<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">name = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>fullname ?? <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>i</mi><mi>r</mi><mi>s</mi><mi>t</mi><mo stretchy="false">?</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">first ?? </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal">rs</span><span class="mord mathnormal">t</span><span class="mclose">??</span></span></span></span>last ?? 'John'; echo $name; // 'John';Code language: PHP (php)

In this example, since the $fullname, $first, and $last doesn’t exist, the $name will take the 'John' value.

PHP null coalescing assignment operator #

The following example uses the null coalesing operator to assign the 0 to $counter if it is null or doesn’t exist:

$counter = $counter ?? 0;Code language: PHP (php)

The above statement repeats the variable $counter twice. To make it more concise, PHP 7.4 introduced the null coalescing assignment operator (??=):

$counter ??= 0;Code language: PHP (php)

In this example, the ??= is the null coalescing assignment operator. It assigns the right operand to the left if the left operand is null. Otherwise, the coalescing assignment operator will do nothing.

It’s equivalent to the following:

if(!isset($counter)) { $counter = 0; }Code language: PHP (php)

In practice, you’ll use the null coalescing assignment operator to assign a default value to a variable if it is null.

Summary #

Did you find this tutorial useful?