php 多个变量赋值是按值还是按引用完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6257131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are multiple variable assignments done by value or reference?
提问by Evil Elf
$a = $b = 0;
In the above code, are both $a
and $b
assigned the value of 0
, or is $a
just referencing $b
?
在上面的代码,都$a
和$b
指定的值0
,或者是$a
只引用$b
?
回答by Bob_Gneu
With raw types this is a copy.
对于原始类型,这是一个副本。
test.php
测试文件
$a = $b = 0;
$b = 3;
var_dump($a);
var_dump($b);
Output:
输出:
int(0)
int(3)
With objects though, that is another story (PHP 5)
但是对于对象,那是另一回事(PHP 5)
test.php
测试文件
class Obj
{
public $_name;
}
$a = $b = new Obj();
$b->_name = 'steve';
var_dump($a);
var_dump($b);
Output
输出
object(Obj)#1 (1) { ["_name"]=> string(5) "steve" }
object(Obj)#1 (1) { ["_name"]=> string(5) "steve" }
回答by Blagovest Buyukliev
Regard this code as:
将此代码视为:
$a = ($b = 0);
The expression $b = 0
not only assigns 0
to $b
, but it yields a result as well. That result is the right part of the assignment, or simply the value that $b
got assigned to.
该表达式$b = 0
不仅分配0
给$b
,而且还产生一个结果。该结果是分配的正确部分,或者只是$b
分配给的值。
So, $a
gets assigned 0
as well.
所以,也$a
被分配0
了。
回答by KingCrunch
You could have tried it yourself
你可以自己尝试一下
$a = $b = 0;
$a = 5;
echo $b;
or
或者
$a = $b = 0;
$b = 5;
echo $a;
(currently I dont really care :D)
(目前我真的不在乎:D)
Thus: No, they are both independent variables with the value 0
.
因此:不,它们都是具有值的自变量0
。
回答by Tomasz Kowalczyk
I'll recommend a good read on this: http://terriswallow.com/weblog/2007/multiple-and-dynamic-variable-assignment-in-php/. In one of comments, you can read:
我会推荐一个很好的阅读:http: //terriswallow.com/weblog/2007/multiple-and-dynamic-variable-assignment-in-php/。在评论之一中,您可以阅读:
It should be noted that if you use multiple assignment on one line to assign an object, the object is assigned by reference. Therefore, if you change the value of the object's property using either variable, the value essentially changes in both.
需要注意的是,如果在一行中使用多重赋值来赋值一个对象,则该对象是通过引用赋值的。因此,如果您使用任一变量更改对象属性的值,则两者的值本质上都会发生变化。
So I'll personally recommend that you assign the variables separately.
所以我个人建议你单独分配变量。
For the record:
作为记录:
$a = $b = 4;
var_dump($a, $b);
$b = 5;
var_dump($a, $b);
Yields:
产量:
int(4)
int(4)
int(4)
int(5)
But:
但:
class Tmp
{
public $foo;
public function __construct()
{
$this->foo = 'bar';
}
}
$a = $b = new Tmp();
var_dump($a, $b);
$a->foo = 'oth';
var_dump($a, $b);
Yields:
产量:
object(Tmp)#1 (1) {
["foo"]=>
string(3) "bar"
}
object(Tmp)#1 (1) {
["foo"]=>
string(3) "bar"
}
object(Tmp)#1 (1) {
["foo"]=>
string(3) "oth"
}
object(Tmp)#1 (1) {
["foo"]=>
string(3) "oth"
}
So the conclusion is that there is no reference for primitives, but there IS a reference to objects.
所以结论是没有对原语的引用,但有对对象的引用。
回答by Taylor Gerring
Both $a and $b are assigned that value of 0. If you wanted $a to reference $b, you would preempt it with an ampersand, e.g.:
$a 和 $b 都被赋值为 0。如果你想让 $a 引用 $b,你可以用一个 & 号来抢占它,例如:
$a = & $b = 0;
回答by kenorb
It depends what're you assigning.
这取决于你分配什么。
If you're assigning a value, then the assignment copies the original variable to the new one.
如果您要赋值,则赋值会将原始变量复制到新变量。
Example 1:
示例 1:
$a = $b = 0;
$b++; echo $a;
Above code will return 0
as it's assignment by value.
上面的代码将返回,0
因为它是按值分配的。
Example 2:
示例 2:
$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
An exception to the usual assignment by value behaviour within PHP occurs with objects, which are assigned by reference in PHP 5 automatically. Objects may be explicitly copied via the clone keyword.
PHP 中通常的按值赋值行为的一个例外发生在对象上,在 PHP 5 中这些对象是通过引用自动赋值的。可以通过 clone 关键字显式复制对象。
Example 3
示例 3
$a = $b = $c = new DOMdocument();
$c->appendChild($c->createElement('html'));
echo $a->saveHTML();
Above code will print <html></html>
.
上面的代码将打印<html></html>
.
回答by mcgrailm
its assigns them both the value of 0
它为它们分配了 0 的值