php7新特性(1)
1.null合并运算符
php7以前经常使用isset()检测三元运算,php7+可以用NULL 合并运算符(??)代替。
说明:NULL 合并运算符会判断变量是否存在且值不为NULL,如果是,它就会返回自身的值,否则返回它的第二个操作数。
写法如下:
// php7以前
$a = isset($_GET['a]) ? $_GET['a'] : 'none';
print($a);
print(PHP_EOL); // PHP_EOL 为换行符
//PHP 7+
$b = isset($_GET['b']) ?? 'none';
print($b);
print(PHP_EOL);
// ?? 链式
$c = $_GET['c'] ?? $_POST['c'] ?? 'none';
print($c);
输出结果为:
none
none
none