PHP 中的 C# 空合并运算符 (??)
2022-08-30 10:20:56
PHP中是否有三元运算符或类似运算符,其作用类似于C#???
??
在C#中是干净和较短的,但在PHP中你必须做这样的事情:
// This is absolutely okay except that $_REQUEST['test'] is kind of redundant.
echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi';
// This is perfect! Shorter and cleaner, but only in this situation.
echo null? : 'replacement if empty';
// This line gives error when $_REQUEST['test'] is NOT set.
echo $_REQUEST['test']?: 'hi';