[thelist] php operator =&

Matt Warden mwarden at gmail.com
Mon Jun 12 14:56:11 CDT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sarah Adams wrote:
> I'm helping someone out with some PHP code, and I keep seeing them using
> the operator "=&", which AFAIK is not a PHP operator... but it seems to
> work fine, as though it were a simple assignment operator (i.e. "=").

Sarah,

You are right. There is no =& operator in PHP.

It's actually two operators.

& says "give me the reference, not a copy"

= is normal assignment.

For example:

$myobj = new MyObj;
$myotherobj = $myobj;
$myotherobj->setSomething('foo');

In this case, 'something' would be set to 'foo' in $myotherobj but not
$myobj. This is because $myotherobj is a copy (at the time of
assignment) of $myobj; Contrast this with:

$myobj = new MyObj;
$myotherobj =& $myobj;
$myotherobj->setSomething('foo');

In this case, $myobj and $myotherobj are essentially aliases for the
same object in memory. Thus, 'something' is set to 'foo' in both instances.

> Before I go telling him that he shouldn't be using it (for the sake of
> code readability, at least), does someone know of some obscure part of
> the PHP docs that mentions this operator? Or perhaps he's confusing it
> with an operator from some other language?

Don't tell him not to use it. It makes sense in a lot of instances for
function's sake, and even more instances for memory/CPU conservation's sake.

hth,

- --
Matt Warden
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEjcbbrI3LObhzHRMRAp9rAKDBY4IkKeWiKxdXaWpx/CNNL4q5RQCgueLB
sgs/9ydrmU93eFoPMTceTgs=
=4CUp
-----END PGP SIGNATURE-----



More information about the thelist mailing list