編程學習網 > 編程教程 > PHP7 教程
2016
09-20

PHP7 Null合并運算符

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

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

示例

<?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

掃碼二維碼 獲取免費視頻學習資料

編程學習