gpt4 book ai didi

php - PHP 变量是按值传递还是按引用传递?

转载 作者:IT老高 更新时间:2023-10-28 11:39:18 25 4
gpt4 key购买 nike

PHP 变量是通过值传递还是通过引用传递?

最佳答案

它是根据 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 - PHP 变量是按值传递还是按引用传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com