PHP 变量是按值传递还是按引用传递? [英] Are PHP Variables passed by value or by reference?
本文介绍了PHP 变量是按值传递还是按引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PHP 变量是按值传递还是按引用传递?
Are PHP variables passed by value or by reference?
推荐答案
It's by value 根据 PHP 文档.
It's by value according to the PHP Documentation.
默认情况下,函数参数是按值传递的(这样,如果函数内参数的值发生更改,则不会在函数外更改).要允许函数修改其参数,它们必须通过引用传递.
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屋!
查看全文