Web Technologies

 null operator (??) in php

It is used to replace the ternary operation in conjunction with isset() function.

// Traditional way
if (isset($y)) {
    echo '$y has value';
} else {
    echo '$y has NO value so lets assign a value as 20..';
    $y = 20;
}

// In terms of ternary operator
$value_y = isset($y) ? $y : 'no value assigned';

// This can be replaced with Null Operator

$value_y = $y ?? 'no value assigned';
// fetch the value of $_GET['user'] and returns 'not passed'
// if username is not passed
   $username = $_GET['username'] ?? 'not passed';

// Equivalent code using ternary operator
   $username = isset($_GET['username']) ? $_GET['username'] : 'not passed';