Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

array to the function, which in turn would pull the items out of the array. Alternatively,
you could write a function that accepted a variable number of arguments, as I have in
Listing 4.6.


Listing 4.6 Function with Variable Number of Arguments


<?
function makeList()
{
print("

    \n");


    for($i=0; $i <func_num_args(); $i++)
    {
    print("

  1. ". func_get_arg($i). "\n");
    }


    print("

\n");
}


makeList("PHP", "MySQL", "Apache");
?>


Recursion


Your functions may make calls to other functions, and they may also make calls to
themselves. The process of a function calling itself is recursion. This circular definition
usually leads to elegant algorithms. The problem is broken down into a small task that's
repeated many times.


Recursive definitions are common in mathematics. Consider this definition of an integer:
the sum or difference between one and any other integer, with one being an integer. Is
three an integer? Yes, because one plus one must be an integer, which is two. And the
sum of one and two must also be an integer.


Recursion is a difficult concept to understand, but it usually leads to clearcode. Take a
look at Listing 4.7. The function checkInteger takes a number as input. We know that
the difference between an integer and one is an integer. So if the function gets a number
bigger than one, it simply checks the number minus one. If we start out with a number
less than zero, we multiply it by negative one and check it. Eventually we will reach one
or a number between zero and one, unless we are passed zero, which is an integer.


Listing 4.7 Using Recursion

Free download pdf