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

Aristona
  • 8,611
  • 9
  • 54
  • 80
  • This code will throw an `E_NOTICE` if `$variable` is not set. So I would say it's better to test `if (!isset($variable)) { $variable = array(); }`. – insertusernamehere Apr 19 '13 at 15:46
  • 2
    I would not say this is a dup. The dup question specifies `?` and `:`, this question is when `?:` are together with no if true. – Ryan B Apr 19 '13 at 15:50

2 Answers2

7

This is shorthand for:

if ($variable) {
    // nothing
} else {
    $variable = array();
}

[] is PHP 5.4 shorthand for array()

Halcyon
  • 57,230
  • 10
  • 89
  • 128
2

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.

Spudley
  • 166,037
  • 39
  • 233
  • 307