Can anyone please explain a recursive function to me in PHP (without using Fibonacci) in layman language and using examples? i was looking at an example but the Fibonacci totally lost me!
Thank you in advance ;-) Also how often do you use them in web development?
Recursion is something that repeats itself. Like a function that calls itself from within itself. Let me demonstrate in a somewhat pseudo example:
Imagine you're out with your buddies drinking beer, but your wife is going to give you hell if you don't come home before midnight. For this purpose, let's create the orderAndDrinkBeer($time) function where $time is midnight minus the time it takes you to finish your current drink and get home.
So, arriving at the bar, you order your first beer and commence the drinking:
Now let's just hope you weren't able to drink enough beer to become so intoxicated that your wife is going to make you sleep on the couch regardless of being home on time -.-
But yeah, that's pretty much how recursion goes.
Basically this. It keeps calling itself until its done
Also works with loops!
You can also trying googling it. Note the "Did you mean" (click on it...). http://www.google.com/search?q=recursion&spell=1
Here is a practical example (there are several good ones already). I just wanted to add one that is useful to almost any developer.
At some point, developers will need to parse an object as with a response from an API or some type of object or array.
This function is initially called to parse an object which may just contain parameters, but what if the object also contains other objects or arrays? This will need to be addressed, and for the most part the basic function already does this, so the function just calls itself again (after confirming that the key or value is either an object or an array) and parses this new object or array. Ultimately what is returned is a string that creates each parameter on a line by itself for readability, but you could just as easily log the values to a log file or insert into a DB or whatever.
I added the
$prefix
parameter to use the parent element to help describe the end variable so that we can see what the value pertains to. It doesn't include things like null values, but this can be amended from this example.If you have the object:
and use:
it will return:
and here is the code to parse it into a string with a line break for each parameter:
This will return the object as follows:
I did the nested switch statements to avoid confusion with
if . . . ifelse . . . else
, but it was almost as long. If it helps, just ask for the if conditionals and I can paste them for those who need it.Walking through a directory tree is a good example. You can do something similar to process an array. Here is a really simple recursive function that simply processes a string, a simple array of strings, or a nested array of strings of any depth, replacing instances of 'hello' with 'goodbye' in the string or the values of the array or any sub-array:
It knows when to quit because at some point, the "thing" it is processing is not an array. For example, if you call replaceHello('hello'), it will return 'goodbye'. If you send it an array of strings, though it will call itself once for every member of the array, then return the processed array.
If you add a certain value (say, "1") to Anthony Forloney's example, everything would be clear:
original:
It work a simple example recursive (Y)