tags:

views:

37

answers:

4

Sometimes I see this code in php :

$car = & new Car();

instead of

$car = new Car();

What's the meaning of the ampersand sign?

+1  A: 

Read this:

PHP Manual: References Explained

Aillyn
+1  A: 

The & assigns by reference -- that is, if you say $a =& $b;, then $a and $b refer to the exact same thing, and changing one will change the other.

For objects, it's mostly a leftover from PHP4 -- back then, PHP used to copy objects when assigning them, and that could cause big problems when you are doing stuff like DOM manipulation. It's rarely (i think never) required with objects in PHP5 (since now objects are assigned by reference anyway), and i think it's even considered deprecated.

cHao
A: 

This assigns by reference instead of by value. I.e., you get a pointer to the object instead of a copy of it. Note:

"Since PHP 5, new returns a reference automatically, so using =& in this context is deprecated and produces an E_STRICT message."

Alex Howansky
A: 

thanks for this

Mike Holmes