W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
在這里,你可以了解更多有關 PHP7 的新特性。
在 PHP7 中,引入了一個新的功能,即空合并運算符(??)。由于在 PHP7 項目中存在大量同時使用三元表達式和 isset() 的情況,因此新增的空合并運算符可以用來取代三元運算與 isset () 函數(shù),如果變量是存在的并且不為 null ,則空合并運算符將返回它的第一個操作數(shù);否則將返回其第二個操作數(shù)。
在舊版的PHP中:isset($_GET[‘id']) ? $_GET[id] : err;而新版的寫法為:$_GET['id'] ?? 'err';
具體的使用參考:
之前版本寫法:
$info = isset($_GET['email']) ? $_GET['email'] : 'noemail';
PHP7版本的寫法:
$info = $_GET['email'] ?? 'noemail';
還可以寫成這種形式:
$info = $_GET['email'] ?? $_POST['email'] ?? 'noemail';
<?php
// fetch the value of $_GET['user'] and returns 'not passed'
// if username is not passed
$username = $_GET['username'] ?? 'not passed';
print($username);
print("<br/>");
// Equivalent code using ternary operator
$username = isset($_GET['username']) ? $_GET['username'] : 'not passed';
print($username);
print("<br/>");
// Chaining ?? operation
$username = $_GET['username'] ?? $_POST['username'] ?? 'not passed';
print($username);
?>
它產生以下瀏覽器輸出 :
not passed
not passed
not passed
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: