views:

23

answers:

1

I need to recursivly echo comments and their respective children from a Jelly collection in Kohana. 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.

+6  A: 

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).

BoltClock
Thank you so much for that, a perfect awnser.
Wolfy87
@BoltClock Nice! +1
Jacob Relkin