Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'&' before the parameter name

Tags:

Just a quick and no doubt easy question. I'm pretty new to PHP and am looking through some existing code. I have tried to find the answer to my question on google, but to no avail.

Can somebody please let me know what the '&' sign before the parameter $var does??

function setdefault(&$var, $default="") {   if (! isset($var))   {     $var = $default;   } } 
like image 913
Tray Avatar asked Feb 28 '09 16:02

Tray


People also ask

What are the 3 apostrophes?

The apostrophe has three uses: 1) to form possessive nouns; 2) to show the omission of letters; and 3) to indicate plurals of letters, numbers, and symbols. ​Do not ​use apostrophes to form possessive ​pronouns ​(i.e. ​his​/​her ​computer) or ​noun ​plurals that are not possessives.

What symbol is apostrophe?

An apostrophe is a punctuation mark (') that appears as part of a word to show possession, to make a plural number or to indicate the omission of one or more letters.


2 Answers

Passes it by reference.

Huh? Passing by reference means that you pass the address of the variable instead of the value. Basically you're making a pointer to the variable.

http://us.php.net/language.references.pass

like image 100
AvatarKava Avatar answered Oct 17 '22 17:10

AvatarKava


It means that the function gets the reference to the original value of the argument $var, instead of a copy of the value.

Example:

function add(&$num) { $num++; }  $number = 0; add($number); echo $number; // this outputs "1" 

If add() would not have the ampersand-sign in the function signature, the echo would output "0", because the original value was never changed.

like image 30
Henrik Paul Avatar answered Oct 17 '22 17:10

Henrik Paul