PHP Recursion and recursive routines

Recursion and recursive routines is a bit more complicated, it can be defined as a regular function that calls itself.


Learning when and how to apply recursion is probably a lot more difficult than just learning what recursion is? and being able to explain recursion effectively in an interview situation is even more difficult one 🙂


For beginners defining recursion is easy – any function that calls itself is a recursive function. Isn’t it?


But do you know what is recursion used for?


Recursion is best used for the case where a large task can be broken down into a repetitive “sub-task”. Because a recursive routine calls itself to perform those sub-tasks.


Factorial is the best example to understand the recursive case.

function factorial($x)
if($x > 1){
/* recursive case */
return factorial($x-1) * $x;
else /* base case */
return 1;


Where $x is the number whose factorial we want to find and that is what recursion is all about – finding repetitive patterns, and breaking a problem down into repetitive sub-tasks.


21 thoughts on “PHP Recursion and recursive routines”

  1. Wow that was odd. I just wrote an really long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Regardless, just wanted to say excellent blog!

  2. Have you ever considered about including a little bit more than just your articles? I mean, what you say is important and all. Nevertheless think about if you added some great pictures or video clips to give your posts more, “pop”! Your content is excellent but with pics and clips, this blog could definitely be one of the best in its niche. Good blog!

  3. Well I truly enjoyed studying it. This article provided by you is very helpful for proper planning.

  4. I’ve read several good stuff here. Definitely worth bookmarking for revisiting. I wonder how much effort you put to make such a excellent informative site.

  5. I think this is one of the most vital info for me. And i’m glad reading your article. But want to remark on few general things, The website style is great, the articles is really excellent : D. Good job, cheers

  6. You should take part in a contest for one of the best blogs on the web. I will recommend this site!

  7. I found your weblog web site on google and examine a number of of your early posts. Continue to maintain up the very good operate. I just extra up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!

  8. Great work! This is the type of information that should be shared around the net. Shame on the search engines for not positioning this post higher! Come on over and visit my web site . Thanks =)

  9. Nice blog here! Also your site loads up very fast! What host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol

  10. While there’s no problem with that, there are going to you should be some issues in the near future.

  11. It comprises a wide range of procedures native to cultures around the world.

  12. What’s up to every one, it’s in fact a good for me to go to see this web page, it includes important Information.

  13. Wow, fantastic blog layout! How long have you been running a blog
    for? you made blogging look easy. The whole look of your site is
    great, let alone the content!

  14. Your mode of explaining everything in this piece of
    writing is truly good, every one can simply understand it,
    Thanks a lot.

  15. Oh my goodness! Impressive article dude! Thank you so much, However I am encountering issues with your RSS. I don’t know the reason why I can’t subscribe to it. Is there anybody having the same RSS problems? Anyone that knows the answer will you kindly respond? Thanx!!

  16. I’ve been browsing on-line greater than 3 hours as of late, but I by no means found any interesting article like yours. It’s lovely price enough for me. In my view, if all webmasters and bloggers made just right content as you did, the net will probably be a lot more useful than ever before.

  17. Great article! That is the kind of info that should be shared
    around the web. Shame on Google for now not positioning this post higher!
    Come on over and seek advice from my web
    site . Thank you =)

  18. Please take care that if the function keeps calling itself, how does it know when to stop? You have to set up a condition, known as a base case. Base cases tell our recursive call when to stop, otherwise it will loop infinitely 🙂

Comments are closed.