是通过值还是引用来完成多个变量分配? [英] Are multiple variable assignments done by value or reference?
本文介绍了是通过值还是引用来完成多个变量分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$a = $b = 0;
在上面的代码中,是否同时为$a
和$b
分配了0
的值,还是$a
仅引用了$b
?
In the above code, are both $a
and $b
assigned the value of 0
, or is $a
just referencing $b
?
推荐答案
使用原始类型,这是一个副本.
With raw types this is a copy.
test.php
$a = $b = 0;
$b = 3;
var_dump($a);
var_dump($b);
输出:
int(0)
int(3)
尽管有了对象,这是另一个故事(PHP 5)
With objects though, that is another story (PHP 5)
test.php
class Obj
{
public $_name;
}
$a = $b = new Obj();
$b->_name = 'steve';
var_dump($a);
var_dump($b);
输出
object(Obj)#1 (1) { ["_name"]=> string(5) "steve" }
object(Obj)#1 (1) { ["_name"]=> string(5) "steve" }
这篇关于是通过值还是引用来完成多个变量分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文