2016
09-20

PHP7 Null合并運(yùn)算符

清華大佬耗費(fèi)三個(gè)月吐血整理的幾百G的資源,免費(fèi)分享!....>>>

在PHP7,一個(gè)新的功能,空合并運(yùn)算符(??)已被引入。它被用來代替三元運(yùn)算并與 isset()函數(shù)功能結(jié)合一起使用。如果它存在并且它不是空的,空合并運(yùn)算符返回它的第一個(gè)操作數(shù);否則返回第二個(gè)操作數(shù)。

示例

<?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);
?>
這將在瀏覽器產(chǎn)生輸出以下結(jié)果-
not passed
not passed
not passed

掃碼二維碼 獲取免費(fèi)視頻學(xué)習(xí)資料

編程學(xué)習(xí)