Recursive functions can be used in PHP and in fact any language, to achieve a variety of goals. A recursive function is defined as one which calls itself (from within itself). A simple example of a recursive function is to achieve the ‘factorial’ mathematical formula. Often represented with a ‘!’ in maths, the factorial of a positive integer (x) is the product of all positive integers less than or equal to x. i.e. 4! = 4 * 3 * 2 * 1 = 25

Calculating the factorial of an integer using a recursive function could be implemented as follows:

    define (DEBUG, 1);

    function f($x) {
        if ($x == 0)
		return 1;
        return $x * f($x-1);

    echo "Final factorial of 7: " . f(7) . "n";

Which when run displays;

Final factorial of 7: 5040