Array in PHP using () or []?


I always use the parentheses to define an array in PHP, such as:

$array = array('a','b','c');

But every now and then I see some code here using brackets, like:

$array = ['a', 'b', 'c'];

However, if I use brackets, I get the error:


Parse error: syntax error, unexpected '[' in ...

What is the correct way? Does it have anything to do with the PHP version? Which version accepts one thing and another?

By giving echo phpversion(); in a PHP page of my hosting I get the following version:



asked by anonymous 01.05.2018 / 00:06

1 answer


According to the documentation, Short array syntax was added in 5.4.0 version of PHP :

$array = ['a', 'b', 'c'];

When you run the above code in a version lower than 5.4 , you will get the error reported, and the other notation has been added in 4 version of PHP a href=""> documentation has already been removed .


What is the right way?

Both are correct if they are being used in a compatible version!


Which version accepts one thing and another?

# PHP 4 ~> 7.x
$array = array('a','b','c');

# PHP 5.4.x ~> 7.x
$array = ['a', 'b', 'c'];
01.05.2018 / 00:25