PHP
  Home arrow PHP arrow Page 3 - Fundamentals of Recursion in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
PHP

Fundamentals of Recursion in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    2006-05-01


    Table of Contents:
  • Fundamentals of Recursion in PHP
  • Introducing the basics of recursion: developing an easy-to-grasp example
  • Emphasizing the fundamentals of recursion: defining an improved recursive function
  • Recursion in a more helpful sense: defining two handy recursive functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Fundamentals of Recursion in PHP - Emphasizing the fundamentals of recursion: defining an improved recursive function
    ( Page 3 of 4 )

    As I said before, recursion is a topic that is best understood by example. Keeping in mind this idea, below I coded a new version of the “arrayToFile()” function that you learned before, which is slightly more understandable. Please have a look at its respective signature:

    function dataToFile($inputData,$file){
        if(is_array($inputData)){
            foreach($inputData as $element){
                // call recursively the 'dataToFile()' function
                dataToFile($element,$file);
            }
        }
        else{
            if(!$fp=fopen($file,'a+')){
                trigger_error('Error opening data
    file',E_USER_ERROR);
            }
            fwrite($fp,$inputData."n");
            fclose($fp);
        }
    }

    In this case, I wrote down this new recursive “dataToFile()” function, which is also handy for saving the contents of a recursive array to a specific text file. As you can appreciate, the function is a bit more compact and readable because I restructured its source code, in order to check whether a given input element is an array, and process it accordingly. In the two recursive functions defined earlier (including the one you saw in the previous section), program control is implicitly returned to the main program by the lines that save the data to the sample file:

    if(!$fp=fopen($file,'a+')){
        trigger_error('Error opening data file',E_USER_ERROR);
    }
    fwrite($fp,$inputData."n");
    fclose($fp);
           

    After defining the above “dataToFile()” recursive function, it’s possible to set up an example that shows how to use it. Here is a simple implementation of this function:

    // define recursive array
    $data=array('element1'=>'A','element2'=>'B','element3'=>array
    ('recursive_element1'=>'C','recursive_element2'=>'D',
    'recursive_element3'=>array('key1'=>'This is an example of
    recursion','key2'=>'This is another example of
    recursion')),'element4'=>'E');
    // call function
    dataToFile($data,'data_file.txt');

    Indeed, the above example is extremely easy to grasp. Basically what I did was build up a new recursive array and use the “dataToFile()” function to save the array elements to file, which is very similar to the example you learned in the previous section.

    As a result of running the previous script, below you can see how all the elements of the $data array are saved to the “data_file.txt” file:

    A

    B
    C
    D

    This is an example of recursion

    This is another example of recursion
    E

    That was pretty cool, since the “dataToFile()” function is capable of traversing a specific recursive array and storing the corresponding elements in a given text file. Now, do you see that defining a recursive function in PHP is not so difficult as it seems? I hope you do.

    At this point, I coded some PHP functions that come handy for demonstrating how to use recursion in PHP. However, I’m not done yet. In the next section, I’ll write a couple of recursive functions that can be a little more helpful when they are included in real PHP applications. Thus, keep on reading to learn how this is achieved.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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