“&” preceeding variable & Passing function parameters by reference variable PHP

& preceeds variable

$original = “foo”;
$ref = &$original;
echo $ref; \\Prints “foo”
// now we change the value of $original
$original = “bar”;
// and then print the value of the referring variable
echo $ref; \\Now prints the new value: “bar”
source: http://www.whypad.com/posts/php-what-is-the-ampersand-preceding-variables/193/

reference variable (&variable)

<?php
function add_some_extra(&$string)
{
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str;    // outputs 'This is a string, and something extra.'
?>

Basically, the argument is treated, referenced by the outside variable, as global
Without the &, it would output ‘This is a string’
source: http://php.net/manual/en/functions.arguments.php

Comments are closed.