PHP 5.4 中提供了哪些数组声明语法的变体


What variants of array declaration syntax are available in PHP 5.4?

在数组的PHP文档页面上,给出的示例代码如下:

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);
// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

这是否意味着第一个代码在 PHP 5.4 中无效,或者第二个代码只是一个替代方案?

第一个代码仍然有效。第二种表示法只是一个更短、更方便的替代方案。

正如其他人所指出的,第一个是有效的。第二个是较短的版本。

以下是第二个版本的优缺点:

Pro
  Good for framework development when dealing with long parameterlists
  Other web languages have similar syntax
  Readable
Contra
  Yet another alias
  Would take distinctness from []
  Not searchable through search engines
  Unreadable
  Patch may be difficult to maintain in future

来自 PHPWiki