Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "&" mean in '&$var' in PHP? [duplicate]

What does "&" mean in '&$var' in PHP? Can someone help me to explain this further. Thank you in advance.

like image 216
frogcoder Avatar asked Dec 12 '22 01:12

frogcoder


1 Answers

It means pass the variable by reference, rather than passing the value of the variable. This means any changes to that parameter in the preparse_tags function remain when the program flow returns to the calling code.

function passByReference(&$test) {
    $test = "Changed!";
}

function passByValue($test) {
    $test = "a change here will not affect the original variable";
}

$test = 'Unchanged';
echo $test . PHP_EOL;

passByValue($test);
echo $test . PHP_EOL;

passByReference($test);
echo $test . PHP_EOL;

Output:

Unchanged

Unchanged

Changed!
like image 107
Tanjima Tani Avatar answered Dec 26 '22 10:12

Tanjima Tani