Home arrow PHP arrow Page 3 - Fundamentals of Recursion in PHP

Emphasizing the fundamentals of recursion: defining an improved recursive function - PHP

Iteration is a straightforward concept. Recursion is a bit more complicated; it can be defined as a regular function that calls itself. PHP supports recursive functions. This article, the first of three parts, will explain recursive functions and help you see why they are useful.

  1. Fundamentals of Recursion in PHP
  2. Introducing the basics of recursion: developing an easy-to-grasp example
  3. Emphasizing the fundamentals of recursion: defining an improved recursive function
  4. Recursion in a more helpful sense: defining two handy recursive functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 23
May 01, 2006

print this article



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){
        foreach($inputData as $element){
            // call recursively the 'dataToFile()' function
            trigger_error('Error opening data

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:

    trigger_error('Error opening data file',E_USER_ERROR);

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
'recursive_element3'=>array('key1'=>'This is an example of
recursion','key2'=>'This is another example of
// call function

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:



This is an example of recursion

This is another example of recursion

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: