PHP Array syntax: array() vs []

Currently there is a funny discussion going on on the php.internals mailing list (Not the first time though). It’s a debate about the syntax of the array notation.
At the moment, when you want to create an array, you’d write:

$myArray = array(); // To initialize it
$myArray = array('value', 'foo', 'bar'); // Initialize, and fill it right away

What now gets proposed, is a shorter notation (some would say, more Javascript-like):

$myNewArray = [];
$myNewArray = ['value', 'foo', 'bar'];

I could probably say I’m in favor of adding the extra array syntax, as it shortens the notation for something as trivial as an array.

2 remarks I have about this, one in favour and one against it:

  • The array() notation as it currently is, is a bit ambiguous. It looks like a function but it’s not. The square brackets notation would be more consistent.
  • As Marcus told, somehow the ‘[]’ array feels not so PHP like… This might be very subjective though.

One response to “PHP Array syntax: array() vs []”