PHP laravel数组错误

How to do that using

$myarray = [1,2];

instead of

$myarray = array(1,2);

does not show an error

Parse error: syntax error, unexpected '[' in C:\...\laravel\public\index.php on line 3

like in laravel-framework? Thank you!

To use that syntax you need PHP >= 5.4.0:

Short array syntax has been added, e.g. $a = [1, 2, 3, 4]; or $a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];.

http://php.net/manual/en/migration54.new-features.php


Btw. It's one of the Laravel requrements:

  • PHP >= 5.4
  • Mcrypt PHP Extension
  • OpenSSL PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension

You can use [] short syntax for arrays if the version is >= 5.4.0.

Syntax

$a = [1, 2, 3];
$b = ['foo' => 'orange', 'bar' => 'apple', 'baz' => 'lemon'];