Home > Article > Backend Development > Passing by reference_PHP tutorial
By default, function arguments are passed by value. If you wish to allow a function to modify the values of its arguments, you can pass them by calling them.
If you want a function argument to be passed by reference, you can prepend Add the ampersand (&) before the parameter name in the function definition:
function foo( &$bar ) {
$bar .= and something extra.;
}
$str = This is a string, ;
foo ($str);
echo $str; // Output This is a string, and something extra.
If you want to pass a call to a function that is not defined this way Parameters, you can add the ampersand (&) before the parameter name in the function call.
function foo ($bar) {
$bar .= and something extra.;
}
$str = This is a string, ;
foo ($str);
echo $str; //output This is a string,
foo (&$str);
echo $str; //output This is a string, and something extra.