本文介紹了PHP的合并函數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
許多編程語言都有合并函數(返回第一個非 NULL 值,示例).遺憾的是,2009 年的 PHP 沒有.
Many programming languages have a coalesce function (returns the first non-NULL value, example). PHP, sadly in 2009, does not.
在 PHP 本身獲得合并函數之前,在 PHP 中實現它的好方法是什么?
What would be a good way to implement one in PHP until PHP itself gets a coalesce function?
推薦答案
php 5.3 中有一個新的操作符可以做到這一點:?:
There is a new operator in php 5.3 which does this: ?:
// A
echo 'A' ?: 'B';
// B
echo '' ?: 'B';
// B
echo false ?: 'B';
// B
echo null ?: 'B';
來源:http://www.php.net/ChangeLog-5.php#5.3.0
這篇關于PHP的合并函數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!