0

I'm not a usual php user but till now I always used to declare arrays in this way:

$arr = ["id" => 15,"val" => 13];

In my local xampp (PHP Version 5.5.9) environment this worked fine, but on server (PHP Version 5.3.28) this code fails giving:

PHP Parse error: syntax error, unexpected '[' in /web/htdocs/site.sit/home/pdo.php on line 24

I switched the declaration to this and everything is ok,

$arr = array("id" => 15,"val" => 13)

But I want to understand why this error occurred As documentation states it is not a matter of deprecated code and I see that the first example is using my first array declaration with the comment note

// as of PHP 5.4

What does it means?

Anyway I suspect that its a problem related with some sort strict mode.

5
  • stackoverflow.com/questions/24550506/… Commented Jul 4, 2014 at 9:27
  • 2
    As of PHP 5.4, right. So after 5.4 you can use [], but before 5.4 (ie your remote server) you have to use () Commented Jul 4, 2014 at 9:28
  • You are yourself saying you know your first syntax works only after 5.4 then how would it work for 5.3.2? Commented Jul 4, 2014 at 9:29
  • PHP is in active development all the time as every other good language. This is an improvement to the array notation as of PHP 5.4. Commented Jul 4, 2014 at 9:29
  • Thx guys. I programmed in php 3 years ago and coming from 6 months of JS it was natural to think that [] was always used. I didnt linked the fact this is an improvement and wasnt possible before 5.4 Commented Jul 4, 2014 at 9:45

1 Answer 1

1

array() has always been the way to declare arrays in PHP since before the dawn of time. In PHP 5.4, the shorter [] has been introduced, simply because it's shorter and many other languages use it too. [] doesn't work in 5.3 or below. TFM documents that.

Sign up to request clarification or add additional context in comments.

1 Comment

AWS users need keep this in mind... The default LAMP on AWS uses PHP 5.3. My local setup uses PHP 5.6 and it took me sometime to find my release to production was failing because the shorter version of array declaration was not understood on my AWS environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.