数组的简写:是否有像 {} 或 [] 这样的文字语法? [英] Shorthand for arrays: is there a literal syntax like {} or []?

查看:46
本文介绍了数组的简写:是否有像 {} 或 [] 这样的文字语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 中数组符号的简写是什么?

我尝试使用(不起作用):

I tried to use (doesn't work):

$list = {};

如果您提供有关 PHP 其他速记的一些信息的链接,那就太完美了.

It will be perfect, if you give links on some information about other shorthands for PHP.

推荐答案

更新:
从 PHP 5.4.0 开始,引入了用于声明数组的缩短语法:

$list = [];

<小时>

上一个答案:

没有.只有 $list = array(); 但是你可以开始添加元素.

There isn't. Only $list = array(); But you can just start adding elements.

<?php
$list[] = 1;
$list['myKey'] = 2;
$list[42] = 3;

就 PHP 而言,这完全没问题.您甚至不会得到未定义变量的E_NOTICE.

It's perfectly OK as far as PHP is concerned. You won't even get a E_NOTICE for undefined variables.

E_NOTICE 级别的错误被发出,以防万一使用未初始化的变量,但不是在这种情况下将元素附加到未初始化的数组.

E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array.

至于速记方法,到处都是.如果您想找到它们,只需阅读手册.

As for shorthand methods, there are lots scattered all over. If you want to find them just read The Manual.

一些例子,仅供娱乐:

  1. $arr[] array_push 的简写.
  2. foreach 结构
  3. echo $string1, $string2, $string3;
  4. 使用 +
  5. 连接数组
  6. elseif
  7. 的存在
  8. 变量嵌入字符串,$name = 'Jack';echo "你好 $name";
  1. $arr[] shorthand for array_push.
  2. The foreach construct
  3. echo $string1, $string2, $string3;
  4. Array concatenation with +
  5. The existence of elseif
  6. Variable embedding in strings, $name = 'Jack'; echo "Hello $name";

这篇关于数组的简写:是否有像 {} 或 [] 这样的文字语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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