php - PHP 有自动激活吗?

标签 php perl autovivification

正在搜索 PHP.net对于自动生成没有结果。在撰写本文时,Wikipedia声称只有 Perl 有它。搜索Google没有明确确定的结果对于“php 自动生成”。

这段 PHP 代码运行良好:

$test['a'][4][6]['b'] = "hello world";
var_dump($test);

array
  'a' => 
    array
      4 => 
        array
          'b' => 
            array
              ...

任何人都可以提供 PHP 确实具有此功能的规范答案(最好有引用资料),以及任何详细信息,例如引入它的版本、怪癖、快捷方式等吗?

最佳答案

是的,PHP 确实具有自动生成功能(并且已经使用了很长时间),尽管该术语并未提及它。 PHP.net状态:

An existing array can be modified by explicitly setting values in it.

This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets ([]).

$arr[key] = value;
$arr[] = value;
// key may be an integer or string
// value may be any value of any type

If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array.

但是,文档指出,如果您尝试访问未设置的数组(或键)的值,它将返回一个错误:

Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an E_NOTICE-level error message will be issued, and the result will be NULL.

我找到了我的旧 PHP3 手册,其中说明了这一点:

You can also create an array by simply adding values to the array.

$a[] = "hello";

关于php - PHP 有自动激活吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5964420/

相关文章:

php - chrome net::ERR_ABORTED 来自多个 .css 文件

javascript - 将值从一个页面的ajax函数传递到另一页面的ajax函数

linux - 使用 Perl 在终端上列出目录

python - 如何为实例更改 dict() 的行为

ruby - 如果哈希 ['a' ] 不存在,如何分配哈希 ['b' ] 'c' ]= ['a'?

php - 使用 php 和 javascript 从 kml 文件创建高程图

java - 数据库连接功能在 PHP 中实现

regex - Perl regex - 删除除字母数字字符和逗号以外的所有字符

perl 检查嵌套哈希引用