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.
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.