De la web: https://www.infoq.com/articles/php-7-array-operators/
Null Coalescing Operator
The isset function can be used to find out if a variable is set and not NULL. Typically you will use the PHP ternary operator with isset as shown in the following example. Here, isset returns true if the GET request parameter name is set, in which case variable its value is assigned to variable $name, otherwise $name is set to a constant string value:
$name = isset($_GET['name']) ? $_GET['name'] : 'Deepak'; echo "Hello " . htmlspecialchars($name)."<br>";
The null coalescing operator (??), which has been added to PHP 7.0, can be used to streamline this kind of operations. Indeed, it will return its first operand if it exists and its value is not NULL, and return the second operand otherwise. The preceding example could be rewritten as follows using ??:
$name = $_GET['name'] ?? 'Deepak';
Null coalescing operators may be chained to return the first defined value:
$name = $_GET['name'] ?? $_POST['name'] ?? 'Deepak'; echo "Hello " . htmlspecialchars($name)."<br>";
Now, create a script ternary.php including all of the following examples:
<?php $name = $_GET['name'] ?? 'Deepak'; echo "Hello " . htmlspecialchars($name)."<br>"; $name = isset($_GET['name']) ? $_GET['name'] : 'Deepak'; echo "Hello " . htmlspecialchars($name)."<br>"; $name = $_GET['name'] ?? $_POST['name'] ?? 'Deepak'; echo "Hello " . htmlspecialchars($name)."<br>"; ?>
If you run the script with no request parameter, all examples will output the last value specified:
Hello Deepak Hello Deepak Hello Deepak
If you instead run the script supplying a request parameter, say name=JohnSmith, all examples will output the request parameter received in $_GET['name']:
Hello JohnSmith Hello JohnSmith Hello JohnSmith
