Recursive function by reference

I need to recursively respond to the comments and their respective children from the Jelly collection in Cohan. I was wondering how to pass a variable to a function through a link. I assume it will be something like this:

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

But I'm not quite sure. So is this correct or when I call a function I don’t need an ampersand?

Thanks.

+4
source share
1 answer

You do not need an ampersand when you call a function because you have already declared that it accepts the link as a parameter using the ampersand.

So you just write this:

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

On a side note, as a rule, you should avoid adding an ampersand to function calls. This is called time call forwarding. It is bad because a function can expect a parameter to be passed by value, but you pass the link instead as if you were clicking on that function without knowing it. As I said above, a function will invariably take a parameter by reference if you declare it as such. Therefore, this makes it unnecessary to go through the link.

In PHP 5.3.0 and newer, link-click time causes PHP to generate E_DEPRECATED warnings because it is deprecated (rightfully).

+10
source

All Articles