PHP Recursive Functions

Description

A recursive function is a function which call itself.

How

Here's a quick overview of how a recursive function operates:

The recursive function is called by the calling code If the base case is met, the function does required calculation, then exits.

Otherwise, the function does required calculation, then calls itself to continue the recursion.

Example

Here is an example to calculate factorials:


<?PHP/*  w w  w .  j ava  2 s . c  o m*/
function factorial($number) { 
        if ($number == 0) return 1; 
        return $number * factorial($number-1); 
} 

print factorial(6); 
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition