正在搜索 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/