What does the =&
(equals-ampersand) assignment operator do in PHP?
Is it deprecated?
What does the =&
(equals-ampersand) assignment operator do in PHP?
Is it deprecated?
It s not deprecated and is unlikely to be. It s the standard way to, for example, make part of one array or object mirror changes made to another, instead of copying the existing data.
It s called assignment by reference, which, to quote the manual, "means that both variables end up pointing at the same data, and nothing is copied anywhere".
The only thing that is deprecated with =&
is "assigning the result of new
by reference" in PHP 5, which might be the source of any confusion. new
is automatically assigned by reference, so &
is redundant/deprecated in$o = &new C;
, but not in $o = &$c;
.
Since it s hard to search, note that =&
(equals ampersand) is the same as = &
(equals space ampersand) and is often written such that it runs into the other variable like $x = &$y[ z ];
or $x = &$someVar
(ampersand dollar sign variable name). Example simplified from the docs:
$a = 3;
$b = &$a;
$a = 4;
print "$b"; // prints 4
Here s a handy link to a detailed section on Assign By Reference in the PHP manual. That page is part of a series on references - it s worth taking a minute to read the whole series.
It s two different operators. =
is assignment as you probably know. And &
means the variable should be accessed by reference rather than by value.
$x = &$y[ z ];
also has the effect of creating $y[ z ]
if it doesn t exist, and setting it to null
.
This prevents error messages that you might have wanted to read. I haven t found documentation on this yet; possibly new in 5.3, for all I know.
The symbol &
is used in various ways in PHP to represent operations with "references". The PHP manual has a section titled References Explained which every PHP programmer should read.
It s important to understand that references in PHP are not a data type, like a pointer, but a concept regarding how variables work. There is therefore no single meaning of &
- you should not read it as "make a reference" - it just means "something reference-y is happening here".
In particular, the syntax $a =& $b
, which can also be written $a = &$b
, represents assignment by reference. It binds two variables together, so that they both point at the same piece of data. Think of the &
as modifying the =
rather than modifying the $b
.
Once you ve bound two variables together in this way, they are interchangeable - you can t say that "$a points to $b" or "$b points to $a":
$a =& $b;
$a = 42;
// both $a and $b will be 42
$b = 101;
// both $a and $b will be 101
You can also link more than two variables together as references, and again it doesn t matter which of the existing names you use on the right-hand side of the assignment:
$a =& $b;
$c =& $b;
$d =& $a;
$e =& $c;
// $a, $b, $c, $d, and $e now all point to the same data, interchangeably
However, if you put the same variable on the left-hand side, it breaks the existing link of that variable, and links it to something else:
$a =& $b;
// $a and $b are linked together
$a =& $c;
// $a is now linked to $c
// the value of $b doesn t change, but it is not linked to $a or $c
To "break" the link without making a new link, you can use the unset
keyword:
$a =& $b;
$c =& $a;
// $a, $b, and $c are all linked together
unset($a);
// $b and $c are still linked together, but $a is independent
Some descriptions refer to =&
as "creating or adding to a reference set". Perhaps it would have been better if it had been implemented as a function, like bind($a, $b)
to highlight that both arguments are affected by the operation.
I d like to draw some attention to the semantics and code styling of "Assigning By Reference". The OP s opening sentence hints toward a misconception:
What does the =& (equals-ampersand) assignment operator do in PHP?
First, let s review the dedicated section of the PHP Docs page for Assignment Operators. Notice how the =
comes before the &
and that the two symbols are separated. This is because they are NOT "combined operators". Semantically, it is "assigning" a "reference"; it is not a "reference assignment operator".
Second, look at how ALL of the "combined operators" are written lower on the docs page. The =
is consistently the right-most symbol. This is a very important distinction because writing the &
on the left of the =
changes the meaning -- it becomes a combined operator ("bitwise and assignment operator") instead of an assignment to a reference.
PSR coding standards should be something that all PHP developers are aware of and strive to obey. Notice this rule of PSR-12 Section 6.2:
All binary arithmetic, comparison, assignment, bitwise, logical, string, and type operators MUST be preceded and followed by at least one space
By this rule, there should always be a space after the =
operator -- this makes =&
a violation.
Furthermore, there are other rules that state that there should not be a space between &
and its variable/argument/function/etc.
When using the reference operator & before an argument, there MUST NOT be a space after it
TL;DR
=
with spaces on both sides and never write a space after &
.$a =& $b;
$a = &$b;
Demonstrated consistently/correctly: https://riptutorial.com/php/example/11991/assign-by-reference
Not demonstrated consistently/correctly:
I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...
<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...
How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...