Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
$variable ?: []
它有什么作用?这是一个三元运算符,但看起来有点不同,我不知道 [] 是什么意思。也许它是 5.4 的简写数组并创建一个空数组?
这是以下的简写:
if ($variable) { // nothing } else { $variable = array(); }
[]是 PHP 5.4 的简写array()
[]
array()
它的意思是:
如果$variable有一个值,则使用该值,否则使用一个空数组。
$variable
[]语法是空数组的 PHP 5.4 简写语法。 PHP 5.3 及更早版本会将其编写为array().
换句话说,是的,您对它的作用的猜测几乎完全正确。