php null coalescing operator empty string code example
Example 1: null coalescing operator php
$username = $_GET['username'] ?? 'not passed';
$username = isset($_GET['username']) ? $_GET['username'] : 'not passed';
Example 2: php null coalesce
$result = $_POST['name'] ?? 'John';
Example 3: php ?? vs ?:
<?php
var_dump(null ?? '`null` is null');
var_dump(null ?: '`null` is empty');
var_dump(true ?? '`true` is null');
var_dump(true ?: '`true` is empty');
var_dump(false ?? '`false` is null');
var_dump(false ?: '`false` is empty');
var_dump('' ?? 'empty string is null');
var_dump('' ?: 'empty string is empty');
var_dump(empty(null) ?: '`null` is not empty');