PHP array() 和 [] 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17772534/
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 16:24:59  来源:igfitidea点击:

PHP Difference between array() and []

phparrayssyntaxsquare-bracket

提问by Mr.Web

I'm writing a PHP app and I want to make sure it will work with no errors.

我正在编写一个 PHP 应用程序,我想确保它不会出错。

The original code:

原始代码:

<?php
$data = array('name' => 'test',
              'id'   => 'theID');

echo form_input($data);
?>

Would the following work with no errors or is not recommended for some reason?

以下是否可以正常工作或出于某种原因不推荐?

<?= form_input(['name' => 'test', 'id' => 'theID']); ?>

Are there any difference?

有什么区别吗?

I've looked again the data about array()and the short array method with square brackets []in PHP.net but I'm not sure.

我再次查看了有关PHP.net 中array()带方括号的短数组方法的数据[],但我不确定。

And also, is the short php tag <?= ?>fine for echoing? Is there any version issue? (provided is enabled in php.ini)

而且,简短的 php 标签<?= ?>是否适合回显?有没有版本问题?(前提是在 php.ini 中启用)

回答by The Alpha

Following []is supported in PHP >= 5.4:

[]PHP >= 5.4 支持以下内容:

['name' => 'test', 'id' => 'theID']

This is a short syntax only and in PHP < 5.4 it won't work.

这只是一个简短的语法,在PHP < 5.4 中它不起作用

回答by David Spector

As of 2019, it has been 7 years since the []syntax was added. That is long enough to drop array()except in old legacy programs, in my opinion.

到 2019 年,[]添加语法已经 7 年了。array()在我看来,除了旧的遗留程序之外,这已经足够长了。

回答by Md. A. Barik

If you are using 5.3 or previous version then you can't use []as an array as well as associative array. If you are using 5.4 or later version of PHP then you can use either array()or []to create an array, associative array or even multidimensional array.

如果您使用的是 5.3 或以前的版本,则不能将其[]用作数组和关联数组。如果您使用 5.4 或更高版本的 PHP,那么您可以使用array()[]来创建数组、关联数组甚至多维数组。

回答by Aydin4ik

And regarding the <?= ?>part of the question: it is largely notfrowned upon, at least not in 2019.

关于问题的<?= ?>一部分:它基本上不会被人反对,至少在 2019 年不会。

  1. A good technical breakdown: https://softwareengineering.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php
  2. A note in PSR-1: Files MUSTuse only <?phpand <?=tags.
  3. TL;DR: There is no reason you cannot or should not use it.
  1. 一个很好的技术分解:https: //softwareengineering.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php
  2. PSR-1 中的一个注释:文件必须只使用<?php<?=标签。
  3. TL;DR:没有理由不能或不应该使用它。

回答by Mahad Ali

Using php 7.2, for me it seems rather then [I am a an array] {I am an array seems to work}. Difference is between {}and []. My code

使用 php 7.2,对我来说似乎而不是 [我是一个数组] {我是一个数组似乎可以工作}。区别在于{}和之间[]。我的代码

<p>
  <label for="post_category"> Cat 1 </label>
  <input type="checkbox" name="post_category{first}" value="cat1">
  <br />
  <label for="post_category{second}"> Cat 2 </label>
  <input type="checkbox" name="post_category" value="cat2">
</p>