Miscellaneous
  Home arrow Miscellaneous arrow Recursion in PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Recursion in PHP
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 17
    2003-03-08

    Table of Contents:
  • Recursion in PHP
  • Factorials
  • Fibonacci
  • Iterative Fibonacci

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Recursion in PHP


    (Page 1 of 4 )

    Learn how recursion works and how to implement it in PHP.

    By : David Stockton

    The first thing you need to do in order to create a recursive function is to create a recursive function unless you already have a recursive function, in which case, just use your recursive function.

    Ok, more seriously, a recursive function is just a function that calls itself. In order to make a recursive function not explode all over itself, though, it is important to keep some ideas in mind.

    The first idea is that some part of the recursive function must have a value that is obtainable without another call to the function. If you don't have this, you run the danger of making an infinite loop, which is a function that continually calls itself forever. Realistically, the computer will run out of memory before forever occurs, or your computer will lose power, or you'll reboot the computer or kill the process, but it can still be a pain.

    So there are several things to keep in mind when you're creating a recursive function:1) You must have an exit condition (the part of the function that can be calculated without further calls to that function)2) Each recursive call must be different than the one before it.3) You must believe it works.

    So, let's try a simple one. The factorial function and the Fibonacci sequences lend themselves easily to recursion, so let's start with the factorial function.

    More Miscellaneous Articles
    More By Codewalkers


       · I know its nit-picking, but the example code above is a perfect example of recursive...
       · Oh. Comments by tutorial rather than by page. The code I am referring to in the...
       · You are correct. It also doesn't check to see that you are passing in a integer or...
       · The code for factorials is not significantly simpler than safe code, just lazier. ...
       · There is an error in listing of last Procedure. In line: " for ($i = 1; $i <...
       · <?phpfunction f($c) { if ($c<2) throw new Exception('Input out of range'); $f...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT