O'Reilly logo

PHP Advanced for the World Wide Web: Visual QuickPro Guide by Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required