ITT #13: Build a Menu with Recursive Functions
To continue my recent obsession with array
handling, I want to spend this week talking about recursive
functions and their application in dealing with arrays.
The goal of our exercise today is to build a menu, complete with
sub-menu, all based off one function that runs recursively.
What Is Recursion?
In computer science, recursion
is a concept in which a function can call itself. This is
similar to the idea of looping in PHP, but it provides an opportunity, in
the case of array handling, to add more fine-grain control into a
program.
On a basic level, recursion can be illustrated with the following code:
function plusOne($x)
{
if($x<10)
{
echo ++$x, "<br />";
plusOne($x);
}
else
{
echo 'Finished! <br />';
}
}
Calling this function will result in the following output:
1
2
3
4
5
6
7
8
9
10
Finished!
Obviously, the above could have been accomplished very simply with a
loop...
read more
- «
-
- 1
-
- »