Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP有自动激活功能吗?_Php_Perl_Autovivification - Fatal编程技术网

PHP有自动激活功能吗?

PHP有自动激活功能吗?,php,perl,autovivification,Php,Perl,Autovivification,搜索自生没有结果。在撰写本文时,有人声称只有Perl拥有它。搜索“php自生”时没有明确的结果 此PHP代码运行良好: $test['a'][4][6]['b'] = "hello world"; var_dump($test); array 'a' => array 4 => array 'b' => array ... 任何人都可以提供一个规范的答案(最

搜索自生没有结果。在撰写本文时,有人声称只有Perl拥有它。搜索“php自生”时没有明确的结果

此PHP代码运行良好:

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

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

任何人都可以提供一个规范的答案(最好是有参考资料的),说明PHP确实具有此功能,以及任何细节,例如它引入的版本、怪癖、快捷方式等?

如果PHP支持自动激活,但您发布的语法在大部分情况下都有效,那么不是100%

// Works as you have assigned a value of 'hello'
$a['a'][4][6]['b'] = "hello";
var_dump($a);
echo print_r($a,true);

// works as you have assigned a value of 'world'
$b[][][][] = "world";
var_dump($b);
echo print_r($b,true);

// ERROR: Cannot use [] for reading
$c[];
var_dump($c);
echo print_r($c,true);

无法使用[]阅读:

是的,PHP确实有自动激活功能(并且已经有很长时间了),尽管该术语没有引用它。国家:

可以通过以下方式修改现有阵列: 在其中显式设置值

这是通过将值指定给 数组,指定中的键 括号。键也可以省略, 导致一对空括号 ([])

如果$arr还不存在,它就会存在 创建,所以这也是一个 创建阵列的替代方法。

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

正在尝试访问数组密钥 没有定义的是相同的 访问任何其他未定义的 变量:E_通知级别的错误 将发出消息,并显示结果 将为空

我已经找到了我的旧PHP3手册,其中说明:

您还可以通过简单地 向数组中添加值

$a[] = "hello";

在perl中,项目将在检查时自动激活,不需要赋值。检查时,将创建到达最内层请求密钥所需的项目路径。请注意,{d=>undef}is条目不是实际创建的,而是隐含的

   use strict;
   use warnings;
   use Data::Dumper;

   my %a;       # as is empty, equivalent to \%a is {};
   print Dumper %a;
   $a{b}{c}{d}; # \%a is now { b => { c => {}}}
                # returns an undef value.
   print Dumper \%a;
输出:

$VAR1 = {};
$VAR1 = {
          'b' => {
                   'c' => {}
                 }
        };
perl数组示例:

use strict;
use warnings;
use Data::Dumper;

my (@b,@c);          # @b=(), @c=()
print Dumper \@b;
$b[3];               # @b=() aka unchanged.
print Dumper \@b;
$b[3][2][1];         # @b=(undef,undef,undef,[undef,undef,[]])
print Dumper \@b;
print Dumper \@c;
$c[3]=1  ;           # @c=(undef,undef,undef,1)
print Dumper \@c;
输出:

Useless use of array element in void context at -e line 7.
Useless use of array element in void context at -e line 9.
$VAR1 = [];
$VAR1 = [];
$VAR1 = [
          undef,
          undef,
          undef,
          [
            undef,
            undef,
            []
          ]
        ];
$VAR1 = [];
$VAR1 = [
          undef,
          undef,
          undef,
          1
        ];

当您分配值时,这将起作用。PHP将为您创建该路径。这只在赋值时有效。@JohnP在pearl的wikipedia链接的示例中似乎也是这样。@JohnP直到不知道pearl代码在做什么。这对我来说都是巫毒:)
perl
,而不是‘pearl’:)我希望我的回答能帮助你理解perl是如何对读取操作进行自动感知的。简单的解释,perl编程语言从一开始就是根据精心设计的设计系统化设计的。PHP并非如此。因此,这个概念在Perl中有一个特定的名称和含义。所以这个功能确实存在,但PHP.net没有命名,对吗?是的,我也将其追溯到PHP3。它被简单地称为“创建数组的替代方法”。
Useless use of array element in void context at -e line 7.
Useless use of array element in void context at -e line 9.
$VAR1 = [];
$VAR1 = [];
$VAR1 = [
          undef,
          undef,
          undef,
          [
            undef,
            undef,
            []
          ]
        ];
$VAR1 = [];
$VAR1 = [
          undef,
          undef,
          undef,
          1
        ];