PHP array() 和 [] 之间的区别 [英] PHP Difference between array() and []

查看:54
本文介绍了PHP array() 和 [] 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 PHP 应用程序,我想确保它可以正常工作且没有错误.

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

原代码:

<?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']); ?>

有什么不同吗?

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

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 标签 <?= ?> 是否适合回显?有没有版本问题?(前提是在 php.ini 中启用)

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

推荐答案

PHP >= 5.4 支持以下[]:

Following [] is supported in PHP >= 5.4:

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

这只是一个简短的语法,并且在 PHP <5.4 不行了.

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

这篇关于PHP array() 和 [] 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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