Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - 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 - Iterative Fibonacci


    (Page 4 of 4 )

    In order to avoid redundant recursive function calls, the Fibonacci sequence is better calculated in an iterative fashion. The algorithm shown below calculates it in a way that would be similar to how you might remember it if you were asked to recite it.

    In order for the algorithm to work, we still need to know that the first two numbers in the sequence are 1 and 1. The algorithm shortcuts this saying the 0th number is 0, and the 1st number is 1. So therefore the second number is 0+1 or 1. From there the algorithm works on a for loop. I've added a second parameter to the function. If the second parameter is not passed in or is not "yes" the function will return only the last number in the sequence. If it is set to "yes" it will return the first $num values in the sequence separated by commas. Without further ado, here's an iterative solution to the Fibonacci sequence:

    <?php
    function fib_iter($num$show_series="no") {
      
    $retval "";
      if (
    $num == ) {
        return 
    1;
      }
      
    $num1=1;
      
    $num2=0;
      
    $retval "1";

      for (
    $i 1$i &lt$num$i++) {
        
    $fib $num2 $num1;
        
    $num2 $num1;
        
    $num1=$fib;
        if (
    $show_series == 'yes') {
            
    $retval .= ", ".$fib;
        }
      }
      if (
    $show_series == 'yes') {
        return 
    $retval;
      } else {
        return 
    $fib;
      }
    }
    echo (
    fib_iter(15"yes"));
    ?>

    About the author

    David Stockton is a programmer for J.D. Edwards who enjoys working with PHP in his spare time. He's been coding for over 16 years and has nearly 4.5 years experience with PHP. He has been happily married for almost 3 years and is looking forward to trying to become a dad. He can be reached for questions or comments or additional tutorial suggestions at codewalker@davidstockton.com.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 2 Hosted by Hostway
    Stay green...Green IT