Are multiple variable assignments performed by value or reference?

$a = $b = 0; 

In the above code, both $a and $b are assigned the value 0 , or is $a just referring to $b ?

+27
variables variable-assignment php
Jun 06 2018-11-11T00:
source share
7 answers

With source types, this is a copy.

test.php

 $a = $b = 0; $b = 3; var_dump($a); var_dump($b); 

Exit

 int(0) int(3) 

With objects, however, this is another story (PHP 5)

test.php

 class Obj { public $_name; } $a = $b = new Obj(); $b->_name = 'steve'; var_dump($a); var_dump($b); 

Exit

 object(Obj)#1 (1) { ["_name"]=> string(5) "steve" } object(Obj)#1 (1) { ["_name"]=> string(5) "steve" } 
+40
Jun 06 '11 at 19:47
source share
β€” -

Pay attention to this code as:

 $a = ($b = 0); 

The expression $b = 0 not only assigns 0 $b , but also gives the result. This result is the right part of the assignment, or simply the value that $b is assigned to.

So $a also gets 0 .

+17
Jun 06 '11 at 19:46
source share

You could try it yourself

 $a = $b = 0; $a = 5; echo $b; 

or

 $a = $b = 0; $b = 5; echo $a; 

(currently I really don't care: D)

Thus: No, they are independent variables with a value of 0 .

+8
Jun 06 '11 at 19:45
source share

I will recommend a good read about it: http://terriswallow.com/weblog/2007/multiple-and-dynamic-variable-assignment-in-php/ . In one of the comments you can read:

It should be noted that if you use multiple assignment in one line to assign an object, the object is assigned by reference. Therefore, if you change the value of objects using any variable, the value changes significantly in both.

Therefore, I personally recommend that you assign variables separately.

For the record:

 $a = $b = 4; var_dump($a, $b); $b = 5; var_dump($a, $b); 

Productivity:

 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); 

Productivity:

 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 are no links to primitives, but there is a link to objects.

+2
Jun 06 '11 at 19:47
source share

Both $ a and $ b are assigned to this value 0. If you want $ a to refer to $ b, you would supplant it with an ampersand, for example:

 $a = & $b = 0; 

http://php.net/manual/en/language.oop5.basic.php

+1
Jun 06 2018-11-11T00:
source share

It depends on what you assign.

If you assign a value, the assignment copies the original variable to a new one.

Example 1:

 $a = $b = 0; $b++; echo $a; 

Above, the code will return 0 as a destination by value.

Example 2:

 $a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4. 

An exception to the usual assignment of behavior to values ​​in PHP occurs with objects that are automatically assigned a reference to PHP 5. Objects can be explicitly copied using the clone keyword.

Example 3

 $a = $b = $c = new DOMdocument(); $c->appendChild($c->createElement('html')); echo $a->saveHTML(); 

<html></html> will be printed above the code.

+1
Feb 21 '15 at 18:05
source share

assigns it to them as the value 0

0
Jun 06 '11 at 19:46
source share



All Articles