开发者

PHP Pass by reference

开发者 https://www.devze.com 2022-12-31 05:35 出处:网络
I am calling this function which is modifying an array by reference: function addWord(&$words, $wordIndex, $word)

I am calling this function which is modifying an array by reference:

function addWord(&$words, $wordIndex, $word)
{
 $words[$wordIndex] = $word;
}

At th开发者_开发问答e function call,

addWord(&$words, $wordsIndex, $word);

($words is used only during the function call)

doesn't work. How do I make this or a similar functionality work? I want the addWord to be a separate function.


call your function without the reference operator:

 $words = array();
 addWord($words, $wordsIndex, $word);


You don't need to pass an ampersand in the function call; only in the function declaration.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号