PHP数组与[]的方法和变量声明 [英] PHP array vs [ ] in method and variable declaration

查看:93
本文介绍了PHP数组与[]的方法和变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP语法有疑问.

I have a question about PHP syntax.

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

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.

变量

public $varname = array();

public $varname = [];

方法

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

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

推荐答案

PHP框架互操作性小组提供)未提及短数组语法.但正如您在中看到的那样第4.3章中,他们使用短数组语法将$arg3的默认值设置为空数组.

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

因此对于PHP> = 5.4,短数组语法似乎是事实上的标准.如果要让脚本在PHP< 5.4上运行,请仅使用array().

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数组与[]的方法和变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆