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; } }
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With