Reference assignment operator in PHP, =&
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.
What do the =& and &= operators in PHP mean?
$a &= $b
is short for $a = $a & $b
which is the bitwise-and operator.
$a =& $b
assigns $a as a reference to $b.
Reference assignment with a ternary operator
try this
$cached ? $s = &$lc : $s = &$lu;
Add some explanation about the code. Sorry for my bad english, I am ashamed to express in English.
If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value.
this is from php expressions manual.
the reason of we can't use $s = $cached ? &$lc : &$lu;
is ternary operator need a expression but &$lc
and &$lu
not a expression.
we can test this code
<?php
$a = 5;
$a;
It's no error.
but this
<?php
$a = 5;
&$a;
throw an error.
Parse error: syntax error, unexpected '&', expecting end of file in /usercode/file.php on line 3
The above is my opinion, I hope you can understand.
It's too difficult to using english for me.(ㄒoㄒ)
Why is the PHP assignment operator acting as an assignment by reference in this case?
The object-oriented part of PHP has been hugely overhauled in PHP 5. Objects are now passed (not exactly but almost) as references. See http://docs.php.net/clone.
Example:
$x1 = new StdClass;
$x1->a = 'x1a';
$x2 = $x1;
$y = clone $x1;
// Performing operations on x2 affects x1 / same underlying object
$x2->a = 'x2A';
$x2->b = 'x2B';
// y is a clone / changes do not affect x1
$y->b = 'yB';
echo 'x1: '; print_r($x1);
echo 'y:'; print_r($y);
prints
x1: stdClass Object
(
[a] => x2A
[b] => x2B
)
y:stdClass Object
(
[a] => x1a
[b] => yB
)
Related Topics
Json_Encode PHP Array as Json Array Not Json Object
PHP Datetime::Modify Adding and Subtracting Months
Html Element Array, Name="Something[]" or Name="Something"
How to Delete a Line from the File With PHP
PHP If Statement With Multiple Conditions
MySQLi Equivalent of MySQL_Result()
How to Create a Simple 'Hello World' Module in Magento
Full Secure Image Upload Script
PHP 5.4 Call-time pass-by-reference - Easy Fix Available
Finding N-Th Permutation Without Computing Others
Json_Encode() Escaping Forward Slashes
Execute PHP Script in Cron Job
MySQLi::MySQLi(): (Hy000/2002): Can't Connect to Local MySQL Server Through Socket 'MySQL' (2)
Convert Command Line Curl to PHP Curl
Simple Jquery, PHP and Jsonp Example