PHP 数组与方法和变量声明中的 []

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26651996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:59:26  来源:igfitidea点击:

PHP array vs [ ] in method and variable declaration

phpstandards

提问by Dieter Gribnitz

I have a question about PHP syntax.

我有一个关于 PHP 语法的问题。

When defining functions and variables, which convention should I use?

定义函数和变量时,应该使用哪种约定?

I know they do the same thing in practice but I would like to know which method conforms to best practice standards.

我知道他们在实践中做同样的事情,但我想知道哪种方法符合最佳实践标准。

Variables

变量

public $varname = array();

or

或者

public $varname = [];

Methods

方法

public function foo($bar = array()){}

or

或者

public function foo($bar = []){}

回答by BreyndotEchse

PSR-2(by PHP Framework Interoperability Group) does not mention short array syntax. But as you can see in chapter 4.3they use short array syntax to set the default value of $arg3to be an empty array.

PSR-2(由PHP Framework Interoperability Group 提供)没有提到短数组语法。但是正如您在第 4.3 章中看到的,它们使用短数组语法将 的默认值设置为$arg3空数组。

So for PHP >= 5.4 the short array syntax seems to be the de-facto standard. Only use array(), if you want your script to run on PHP <5.4.

所以对于 PHP >= 5.4,短数组语法似乎是事实上的标准。array()如果您希望脚本在 PHP <5.4 上运行,请仅使用。

回答by Nitsan Baleli

from PHP docs:

来自 PHP文档

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

从 PHP 5.4 开始,您还可以使用短数组语法,它将 array() 替换为 []。

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

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

if you'd try using '[]' notation in earlier version than 5.4, it will fail, and PHP will throw a syntax error

如果您尝试在 5.4 之前的版本中使用 '[]' 表示法,它将失败,并且 PHP 将抛出一个 syntax error

for backward-compatibility you should use array()syntax.

为了向后兼容,您应该使用array()语法。

回答by Valentin Rodygin

That depends on what version of PHP you are targeting. For the best backward compatibility, I'd recommend you to use array(). If you don't care about older versions (< PHP 5.4), I'd recommend you to use a shorter version.

这取决于您所针对的 PHP 版本。为了获得最佳的向后兼容性,我建议您使用 array()。如果您不关心旧版本(< PHP 5.4),我建议您使用较短的版本。

回答by Aurangzeb

just made a test to see how [] performs vs array() here is what I got

刚刚做了一个测试,看看 [] 如何执行 vs array() 这就是我得到的

testing 1 million []s ( with $arr[] = [] )
started at : 1561298821.8754
ended at: 1561298822.6881
difference: 0.81266021728516 seconds 

testing 1 million array()s ( with array_push( $arr, array()) )
started at : 1561298822.6881
ended at: 1561298823.843
difference: 1.1549289226532 seconds 

testing 1 million []s ( again while the processor is hotter)
started at : 1561298823.8431
ended at: 1561298824.7448
difference: 0.9017219543457 seconds 

so [] performed about 20% faster

所以 [] 的执行速度提高了大约 20%