$variable ?: []
What does it do? It's a ternary operator but looks a bit different and I don't know what [] means. Perhabs it's shorthand array of 5.4 and creates an empty array?
$variable ?: []
What does it do? It's a ternary operator but looks a bit different and I don't know what [] means. Perhabs it's shorthand array of 5.4 and creates an empty array?
This is shorthand for:
if ($variable) {
// nothing
} else {
$variable = array();
}
[]
is PHP 5.4 shorthand for array()
It means:
If $variable
has a value then use that value, otherwise use an empty array.
The []
syntax is PHP 5.4 shorthand syntax for an empty array.
PHP 5.3 and earlier would write it as array()
.
In other words, yep, your guess in the question about what it does is pretty much exactly right.