$name = $_GET['name'] ?? $_POST['name'] ?? 'nobody';
//it is equavelent to below conditions
if (isset($_GET['name'])) {
$name = $_GET['name'];
} elseif (isset($_POST['name'])) {
$name = $_POST['name'];
} else {
$name = 'nobody';
}
<?php
// Example usage for: Null Coalesce Operator
$action = $_POST['action'] ?? 'default';
// The above is identical to this if/else statement
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else {
$action = 'default';
}
?>
// if $_POST['name'] doesn't exist $result will equal to John
$result = $_POST['name'] ?? 'John';