Recursion

Rob Allen
08/25/2008 04:19 pm

PHPWomen.org recently held an article-writing contest on their Best Practices Forum. Authors of the two winning submissions each received copies of Zend Studio for Eclipse, a 1-year subscription to Linux Pro Magazine (which is called Linux Magazine outside North America), and the opportunity to feature their articles on the magazine websites. Congratulations goes to Rob Allen for his winning submission!

Recursion is the term used when an operation is repeated on the results of the same operation. That's a little confusing, but what it means to a programmer is that you have a function that calls itself.

Consider the process of adding up numbers in an array such as:

<?php
$array = array(10, 21, 4);
?>

All we need to do is iterate over the array and add each number to a variable as shown here:

<?php
function sumArray($array)
{
$total = 0;
foreach ($array as $element) {
$total += $element;
}
return $total;
}
$array = array(10, 21, 4);
$result = sumArray($array);
echo "result = $result\n";
?>

As you would expect, the output of the script is:

result = 35

But, what if the array is nested? For example, an array like this:

<?php
$array = array(10, 20, 5,
array(5, 2, 3)
);
?>

As we iterate over this new array, we will come to an element that is itself an array. We need to sum up the elements within this sub-array and, fortunately, we have just written a function that does just that (sumArray() !), so let's call it within the foreach() loop:

<?php
function sumArray($array)
{
$total = 0;
foreach ($array as $element) {
if(is_array($element)) {
$total += sumArray($element);
} else {
$total += $element;
}
}
return $total;
}
?>

This is recursion as we have called the sumArray() function from within the sumArray() function itself. That is, sumArray() is a recursive function.

We can now use our improved function to add up our nested array:

<?php
$array = array(10, 20, 5,
array(5, 2, 3)
);
$result = sumArray($array);
echo "result = $result\n";
?>

which will now output:

result = 45

As sumArray() is a recursive function, it will happily handle an array that is many nested levels deep. For example, consider an array that is nested 5 levels deep:

<?php
$array = array(10, 20, 5,
array(5, 2, 3,
array(5, 3,
array(2, 10,
array(19, 1)
),3
), 2, 7
), 3
);
$result = sumArray($array);
echo "result = $result\n";
?>

When this code is run, the output is:

result = 100

Clearly recursion is a powerful and flexible technique for solving problems involving nested data such as reading through XML structures or handling a tree within a database table (usually implemented with a parent_id column). It is also helpful when writing a sorting algorithm, but most PHP programmers don't need to do that! Recursive solutions also tend to be fairly compact and easy to debug as you only need to do it once!

Obviously, as we're programming, there are trade-offs involved! Recursive solutions are inefficient in terms of performance, so consider caching the result. Also, it's possible to get into situation where the recursion never stops. Always make sure that your function will end! Related to that, when you have deeply nested recursion, you can run out of "stack space" (this area reserved for the list of functions that are currently being called. In other words - make sure you test thoroughly :)

Go on.. write a recursive function today!