Answers for "null coalescing operator php"

PHP
2

null coalescing operator example in php

$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';
}
Posted by: Guest on November-24-2021
0

php 7

<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>
Posted by: Guest on May-27-2020

Browse Popular Code Answers by Language