带数组的php引用概念
&``符号在PHP中如何用于数组 当我执行下面的代码时带数组的php引用概念,php,arrays,Php,Arrays,&``符号在PHP中如何用于数组 当我执行下面的代码时 <?php $cfg=array(); $curpath=array(); $name="check"; array_push($curpath, strtolower($name)); $ptr =& $cfg; /*what happens here*/ $ptr =& $ptr[$name]; print("\ncfg\n"); print
<?php
$cfg=array();
$curpath=array();
$name="check";
array_push($curpath, strtolower($name));
$ptr =& $cfg;
/*what happens here*/
$ptr =& $ptr[$name];
print("\ncfg\n");
print_r($cfg);
?>
请解释以下陈述
$ptr = &$cfg;
/*what happens here*/
$ptr =& $ptr[$name];
这里,
&
表示数组是通过引用分配的。这意味着不是将值从$cfg
复制到$ptr
,而是实际引用完全相同的数组。如果使用$ptr
修改阵列,则在使用$cfg
访问阵列时将看到这些更改。同样,如果使用$cfg
修改数组,则使用$ptr
访问数组时将看到更改
没有$ptr=&$cfg中的&
,则$cfg
的值将被复制到$ptr
。在这种情况下,将有两个完全不同的数组。然后,对$ptr
的更改将不会反映在$cfg
中,反之亦然
例如,如果我们有:
$cfg = ["item 1" => 1, "item 2" => 2];
$ptr = &$cfg;
$ptr["item 1"] = 999;
echo $cfg["item 1"];
echo $ptr["item 1"];
输出将是:
999
999
但是,如果将$ptr=&$cfg
更改为$ptr=$cfg
,则输出将为:
1.
999
在第二种情况下,原始$cfg保持不变。您的示例对我来说没有意义,但让我们看看:
$config = array();
$currentPath = array();
// you pushed "check" into the end of the $currentPath array
$name = "check";
array_push($currentPath, strtolower($name));
echo "currentPath:\r\n";
var_dump($currentPath);
// array (
// 0 => 'check',
// )
echo "config:\r\n";
var_dump($config);
// array(0) {
// }
// $ptr (pointer) is actually a reference.
// you now have a $reference to $config.
// (btw it should be written $foo = &$bar and NOT $foo =& $bar)
$reference = &$config;
// "what happens here"
// $reference is empty. so $reference["check"] does not exist.
// you create an offset "check" at $reference, so at $config.
// (i have no idea how - if somebody know why please let me know)
$reference = &$reference[$name];
// what happen here is
// - offset $config["check"] created
// - the reference of $config["check"] has been assigned to $reference (overwritten previous $reference var)
echo "config:\r\n";
var_dump($config);
// array(1) {
// ["check"]=>
// &NULL
// }
// $reference points now to $config["check"], so ...
$reference = 123;
// should set $config["check"] to 123
echo "config:\r\n";
var_dump($config);
// array(1) {
// ["check"]=>
// &int(123)
// }
一个更“现实”的例子可以是:
// empty config
$config = array();
// ...
// create config offset "check" with default value NULL
$name = 'check';
$config[$name] = null;
// ...
var_dump($config);
// array(1) {
// ["check"]=>
// NULL
// }
// using a reference to $config["check"] (for w/e reasons)
$check = &$config[$name];
// ...
// update $config["check"]
$check = 123;
var_dump($config);
// array(1) {
// ["check"]=>
// &int(123)
// }
unset($check); // release reference
var_dump($config);
// array(1) {
// ["check"]=>
// int(123)
// }
@为什么PHP要创建偏移量
$config[“check”]
执行$reference=&$reference[$name]时代码>?
PHP不应该提出一个未定义的索引通知吗 谢谢。另一个疑问是,&$reference[$name]”在没有“check”时会抛出错误。另外,请建议打印$reference时会发生什么。这是第二步。
// empty config
$config = array();
// ...
// create config offset "check" with default value NULL
$name = 'check';
$config[$name] = null;
// ...
var_dump($config);
// array(1) {
// ["check"]=>
// NULL
// }
// using a reference to $config["check"] (for w/e reasons)
$check = &$config[$name];
// ...
// update $config["check"]
$check = 123;
var_dump($config);
// array(1) {
// ["check"]=>
// &int(123)
// }
unset($check); // release reference
var_dump($config);
// array(1) {
// ["check"]=>
// int(123)
// }