3

I was unable to find an answer to this situation that I have stumbled uppon this morning. I have declared some php variables as I usually do in javascript (by mistake). I was surprised that the code actually worked. I have tested it under Linux and it works fine. Under WAMP on Windows does not work.

The code:

<?php

error_reporting(E_ALL);

$phones = [];

var_dump($phones);

?>

Result in Linux:

array(0) {
}

Result in Windows:

Parse error: syntax error, unexpected '[' in D:\wamp\www\test.php on line 7

Question: Is this a valid declaration of an array in php?

0

3 Answers 3

6

PHP 5.4.0 offers a wide range of new features: Read more

Short array syntax has been added,

e.g.

$a = [1, 2, 3, 4]; or $a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];

Above syntax is not supported below versions than 5.4.0. According to my understanding You might be running different versions of PHP in Linux & Windows. Use phpinfo() to check the PHP versions.

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

1 Comment

I have accepted your question because it had the link, even though @Ardy Dedase had answered first.
6

The [] syntax is valid only in PHP 5.4 or newer, check your PHP version on windows.

Comments

2

You should check the PHP Version on both your Linux and Windows machines.

I suspect that your Linux has PHP 5.4+ and Windows has PHP 5.3.x or lower.

I suggest you use array() instead of the short array syntax [] which is only valid in 5.4.

1 Comment

Yes, it is exactly like that.

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.