View Full Version : & before a variable

05-08-2003, 07:21 PM
At times I see variables like so:

&$array with the "&" before the variable name. I couldn't find an explanation of it anywhere, could anyone explain what it stands for?


05-08-2003, 10:09 PM
The "&" before the variable name is used to assign a reference to that variable. By default, PHP assigns all variables by value, which is quite dumb when you work a lot with objects and very inconsistent in regard to other OOP languages. Gladly, this matter is subject to change in PHP5.

But back to references: They are quite good explained at http://www.php.net/manual/en/language.references.whatdo.php, so I only want to point out that should be aware that in newer PHP versions you only define references as arguments during the function definition and not at runtime by invoking the function, i.e.

function foo(&$bar) {
do sth.

$test =& new Bar();

is correct syntax, whereas

function foo($bar) {
// do sth.

$test =& new Bar();

would be not good in newer PHP versions and prompt an error notice. I know that's kinda confusing, so if you feel like... don't hesitate to ask. :)

05-08-2003, 10:16 PM
yeah, makes sense. I just didn't know it meant it was a reference.. :thumbsup:

Helps that you explained which way to use it in this language as well.