FireworksColdFusionDreamweaverFreehandFlashMXHome
Latest New Content

Latest Free Content
View All
Free Content
Accessibility
CMX Learning Guides
Hosted by enterhost

Introducing Recursive Functions in PHP

By: Rob Williams

Page 1 of 3

Set for printing

Next

One of the basic building blocks of any object-oriented programming language is the function. Functions allow you to reuse blocks of code and perform common tasks from other points within a script. In addition to straight calls though, functions can also be created to act recursively, adding a whole new dimension and power to this basic programming building block.

What is a recursive function?

A recursive function is one that calls an instance of the same function from within itself — it recurs. Although that may sound like a rather strange and disastrous thing to do there are times when this type of recursion can overcome problems and handle scenarios that would not otherwise be possible.

A Programmer's Analogy: Loops

The easiest way to think of the benefits of recursive functions is to compare them to loops. Let's take a look at a few examples to demonstrate this point.

Ordinarily a statement will only get executed once.

<?php

echo ("This statement only happens once");

?>

There are, of course, times when you'll want to use the same statement or block of code multiple times; a loop allows you to do this.

<?php
//10 statements
//Without a loop
echo ("This statement will appear 10 times");
echo ("This statement will appear 10 times");
echo ("This statement will appear 10 times");
echo ("This statement will appear 10 times");
echo ("This statement will appear 10 times");
echo ("This statement will appear 10 times");
echo ("This statement will appear 10 times");
echo ("This statement will appear 10 times");
echo ("This statement will appear 10 times");
echo ("This statement will appear 10 times");


//With a loop
for ($i = 0; $i < 10; $i++) {
   echo ("This statement will appear 10 times");
}

?>

By utilizing loop functionality we're able to make our one statement or block of code much more flexible and adaptable. Imagine, for example, that we want to change the text that gets output in the above example. The code without the loop requires that we now change 10 lines of script in order to make the change; the loop code only needs to have one line updated. The loop is also much more adaptable than the static code. If, for example, we wanted to output 20 lines instead of 10 all that we need to do is change the maximum loop counter; the static code requires that we now type 10 additional lines of code. More importantly we could potentially use the loop to continually loop over a block of code until a certain condition has been met; this makes the loop much more flexible and easier to work with than static code.

<?php
$theString = "Loops are a programmer's best friend";
//Output 1 character at a time until the entire string
//has been output.


//Looping code (based on condition)
while (strlen($theString) > 0) {
   //Output the first character
   echo (substr($theString, 0, 1));

   //Delete the first character from the string
   $theString = substr($theString, 1);
}

//Static code: possible, but must be manually updated
//if the length of the string is changed.
echo (substr($theString, 0, 1));
$theString = substr($theString, 1);
echo (substr($theString, 0, 1));
$theString = substr($theString, 1);
echo (substr($theString, 0, 1));
$theString = substr($theString, 1);
echo (substr($theString, 0, 1));
$theString = substr($theString, 1); //etc etc

?>

Page 1 of 3 1 2 3 Next


download
Download Support Files


Keywords
Recursive function, array, multi-dimensional, JavaScript, generate XML