默认情况下,函数参数是按值传递的(因此,如果函数中参数的值发生更改,则不会在函数外部进行更改).要允许函数修改其参数,必须通过引用将其传递.
By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.
要使函数的参数始终通过引用传递,请在函数定义中的参数名称前加上与号(& ).
To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition.
<?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.'
?>
这篇关于PHP变量是按值还是按引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!