Function Recursion and Static Variables

In your programming you've most certainly had occasion to define and use your own functions—it's a wonderful tool, helping to organ ize your code and saving you time. What you may not have experimented with is the capability to use recursion with your functions.

Recursion is the act of a function calling itself.

function function_name () { 
   // Other code.
   function_name ();
}

The end result is that your functions can act both as originally intended and as a loop. The one huge warning when using this technique is to make sure your function has an “out” clause. For example, the following code will run ad infinitum.

function add_one ($n) { 
   $n++;
   add_one ($n);
}
add_one (1);

The lack of a condition that determines ...

Get PHP Advanced for the World Wide Web: Visual QuickPro Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.