Use PHP Recursive Functions to create PHP code that dynamically grows with your data. This will save you time and confusion, and understanding PHP recursive functions will make your PHP code much more scalable. At first, it may be a confusing concept to grasp (especially for PHP beginners).
Learn how to create recursive functions by taking a look at this sample PHP code.
PHP Recursive Function
Last week we went through how to create PHP variable variables. The last piece of PHP sample code had many different loops inside of loops. The code worked, but it was not very efficient because of the following:
- It only supported an array that was three levels deep (in other words, it is not scalable). If you wanted to go deeper, you would have to create more loops inside of loops.
- The PHP code is not easy to work with if you need to make changes.
- Debugging this kind of code can be difficult and time consuming.
This is where PHP recursive functions come in. They allow you to create a function or method that calls itself, and allows you to create code that can handle unlimited depths.
I took the PHP sample code from the PHP Variable Variables tutorial, and created a recursive function. Keep in mind that you do not need to use a method/class…you can use a function. In this case I went with a method because I had scope issues with the data I wanted accessible outside of the class.
<?php /* * The Config class is meant to handle the processing of the config * array, along with storing the values in the configuration as * properties of this object. */ class Config { /* * This starts the whole process of going through the * config array. */ function processConfig($config) { foreach($config as $configItem => $configValue) $this->configProcess($configItem, $configValue); } /* * This is our recursive function that goes through each item in the * config and see if there are any additional layers to process. This * will print the name of the config item and the value. */ function configProcess($configKey, $configValue) { if(!empty($configValue)) { echo '<ul>'; if(is_array($configValue)) { foreach($configValue as $configItem2 => $configValue2) { $varName2 = $configKey . '_' . $configItem2; $this->$varName2 = $configValue2; echo '<li>' . $varName2 . ': ' . $this->$varName2; // This check prevents duplicates from displaying on // the screen... if(is_array($configValue2)) $this->configProcess($varName2, $configValue2); echo '</li>'; } } else { $this->$configKey = $configValue; echo '<li>' . $configKey . ': ' . $this->$configKey . '</li>'; } echo '</ul>'; } } } $configOptions = array( 'database' => array( 'dbName' => 'ChrisDatabase', 'dbUsername' => 'ChrisUser', 'dbPassword' => 'ChrisPass' ), 'photos' => '/path/to/photos/', 'videos' => array( 'gallery1' => array( 'name' => 'Gallery #1', 'path' => '/path/to/gallery/'), 'gallery2' => array( 'name' => 'Gallery #2', 'path' => '/path/to/gallery/'), 'gallery3' => array( 'name' => 'Gallery #3', 'path' => '/path/to/gallery/') ) ); $config = new Config; $config->processConfig($configOptions); echo '<pre> ' . $config->database_dbName . ' ' . $config->photos . ' ' . $config->videos_gallery1_name . ' </pre>'; ?>
In the configProcess() method you will notice there is only one foreach loop. Line #41 is what makes the method a recursive function, because it calls itself. It will infinitely call itself until the value is not an array (at which point the function is returned). Just be very careful in not making code that runs infinitely!
A real world scenario where I have used a PHP recursive function would be displaying a category structure that supports parent categories (displaying a tree like structure).
How have you used PHP recursive functions?
Sample PHP Code: PHP Recursive Functions…
Use PHP Recursive Functions to create PHP code that dynamically grows with your data. This will save you time and confusion, and understanding PHP recursive functions will make your PHP code much more scalable. At first, it may be a confusing concept t…
LikeLike
Google recursion. 🙂
LikeLike
fg
LikeLike
i need to know more about php code
LikeLike
how can i expand php array?
LikeLike