Skip to the content.

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

The null coalescing operator (??) is a shorthand operator in many programming languages, including PHP, JavaScript, and others. It provides a concise way to handle null or undefined values by returning the first operand that is not null or undefined.

The null coalescing assignment operator (??=) is a shorthand syntax in programming languages that allows you to assign a default value to a variable only if the variable is currently null or undefined. It’s a combination of the nullish coalescing operator (??) and the assignment operator (=).

//// Usage of the ?? operator instead of isset()

$var = $_GET['var'] ??= 2023; // CASE 1
// CASE 1
if (!isset($_GET['var'])) {
  $_GET['var'] = 2023;
  $var = 2023;
}

$var = $_GET['var'] ?? 2023; // CASE 2
// CASE 2
if (!isset($_GET['var'])) {
  $var = 2023;
}

Explanation: