Note: Only the difference in using [] or array() is with the version of PHP you are using. In PHP 5.4 you can also use the short array syntax, which replaces array() with [].
Advertisements. An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.
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.
In PHP, elements can be added to the end of an array by attaching square brackets ([]) at the end of the array's name, followed by typing the assignment operator (=), and then finally by typing the element to be added to the array. Ordered Arrays.
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.
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.
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.
And regarding the <?= ?>
part of the question: it is largely not frowned upon, at least not in 2019.
<?php
and <?=
tags.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With