开发者

Recursive function via reference

I need to recursivly echo comments and their respective children from a Jelly collection in Ko开发者_开发技巧hana. I was wondering how I pass a variable to a function via reference. I assume it would be something like this:

function recursive(&$array)
{
    recursive(&$array);
}

But I am not quite sure. So is this correct or when I call the function do I not need the ampersand?

Thanks.


You don't need the ampersand when you call the function because you have already declared it to accept a reference as a parameter using the ampersand.

So you would just write this:

function recursive(&$array)
{
    recursive($array);
}

On a side note, generally you should avoid adding the ampersand to function calls. That is called call-time pass-by-reference. It's bad because the function may be expecting a parameter to be passed by value, but you're passing a reference instead so in a way you're screwing with the function without it knowing. As I have said above, a function will invariably take a parameter by reference if you declare it as such. That therefore makes call-time pass-by-reference unnecessary.

In PHP 5.3.0 and newer, call-time pass-by-reference causes PHP to emit E_DEPRECATED warnings as it has been deprecated (rightfully so).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜