Last active
August 13, 2019 15:30
-
-
Save slav123/f5bfd9323e559de9402368130eed4a8d to your computer and use it in GitHub Desktop.
Null coalescing operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment