PHP Difference between array() and []

Solution 1:

Following [] is supported in PHP >= 5.4:

['name' => 'test', 'id' => 'theID']

This is a short syntax only and in PHP < 5.4 it won't work.

Solution 2:

As of 2019, it has been 7 years since the [] syntax was added. That is long enough to drop array() except in old legacy programs, in my opinion.

Solution 3:

If you are using 5.3 or previous version then you can't use [] as an array as well as associative array. If you are using 5.4 or later version of PHP then you can use either array() or [] to create an array, associative array or even multidimensional array.

Solution 4:

And regarding the <?= ?> part of the question: it is largely not frowned upon, at least not in 2019.

  1. A good technical breakdown: https://softwareengineering.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php
  2. A note in PSR-1: Files MUST use only <?php and <?= tags.
  3. TL;DR: There is no reason you cannot or should not use it.