PHP's =& operator
Asked Answered
D

6

45

Are both these PHP statements doing the same thing?:

$o =& $thing;

$o = &$thing;
Dennis answered 8/5, 2011 at 20:31 Comment(0)
G
40

Yes, they are both the exact same thing. They just take the reference of the object and reference it within the variable $o. Please note, thing should be variables.

Gillead answered 8/5, 2011 at 20:36 Comment(1)
Yes, sorry I did mean $thing.Dennis
B
30

They're not the same thing, syntactically speaking. The operator is the atomic =& and this actually matters. For instance you can't use the =& operator in a ternary expression. Neither of the following are valid syntax:

$f = isset($field[0]) ? &$field[0] : &$field;
$f =& isset($field[0]) ? $field[0] : $field;

So instead you would use this:

isset($field[0]) ? $f =& $field[0] : $f =& $field;
Brubeck answered 15/7, 2012 at 1:47 Comment(2)
I was hoping to find some documentation to confirm this, however php.net isn't very clear. What references do mentions =&, but, oddly, =& doesn't actually apear in the assignment operators (it's always $x = &$y;). Is the white space really significant or are these identical? Can you provide some references?Frangipani
If the PHP folks wanted, they could modify the PHP parser to allow the second bad example above, but it looks like the =& combination is interpreted by the preprocessor and not at runtime. If you peek at the source code of the PHP compiler/interpreter you can confirm this.Brubeck
U
13

They both give an expected T_PAAMAYIM_NEKUDOTAYIM error.

If you meant $o = &$thing; then that assigns the reference of thing to o. Here's an example:

$thing = "foo";

$o = &$thing;

echo $o; // echos foo

$thing = "bar";

echo $o; // echos bar
Undesigning answered 8/5, 2011 at 20:36 Comment(1)
Yes, sorry I did mean $thing.Dennis
B
12

The difference is very important:

<?php
$a = "exists";
$b = $a;
$c =& $a;
echo "a=".$a.", b=".$b.", c=".$c."<br/>"; //a=exists b=exists c=exists

$a = null;
echo "a=".$a.", b=".$b.", c=".$c; //a= b=exists c= 
?>

Variable $c dies as $a becomes NULL, but variable $b keeps its value.

Bonar answered 19/2, 2015 at 5:54 Comment(1)
That's not what the (current edit) of the question is asking.Carlin
S
1

Yes, they do. $o will become a reference to thing in both cases (I assume that thing is not a constant, but actually something meaningful as a variable).

Subteen answered 8/5, 2011 at 20:36 Comment(4)
You don't reference functions like that, nor can you reference constants.Fluter
Karl, I assumed that thing was NOT a constant since references to constants are pointless. You are correct about the functions though.Leu
Yes, sorry I did mean $thing.Dennis
This answer is obsolete, it was an answer to a former version which was errorenous forulated please delete thisRatify
F
1

If you meant thing with a $ before them, then yes, both are assigning by reference. You can learn more about references in PHP here: http://www.php.net/manual/en/language.references.whatdo.php

Fluter answered 8/5, 2011 at 20:38 Comment(2)
Yes, sorry I did mean $thing.Dennis
This answer is obsolete, it was an answer to a former version which was errorenous forulated please delete thisRatify

© 2022 - 2024 — McMap. All rights reserved.