Created
May 21, 2018 08:45
-
-
Save HavenShen/815d99264b2ee3d5b4be08608db7769e to your computer and use it in GitHub Desktop.
由于日常使用中存在大量同时使用三元表达式和 isset() 的情况,NULL 合并运算符使得变量存在且值不为 NULL, 它就会返回自身的值,否则返回它的第二个操作数。
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 | |
/* | |
|-------------------------------------------------------------------------- | |
| NULL 合并运算符 | |
|-------------------------------------------------------------------------- | |
| | |
| 由于日常使用中存在大量同时使用三元表达式和 isset() 的情况,NULL 合并运算符使得变量存在且值不为 NULL, 它就会返回自身的值,否则返回它的第二个操作数。 | |
| | |
*/ | |
// PHP 7 之前 | |
$name = isset($_GET['name']) ? $_GET['name'] : 'Anonymous'; | |
echo $name; | |
// PHP 7 之后 | |
$name = $_GET['name'] ?? 'Anonymous'; | |
echo $name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment