Page 199 - Beginning PHP 5.3
P. 199

Chapter 7: Functions
                           Recursion is another technique that you can use if you need to work on a set of values. Generally
                         speaking, it ’ s usually easier to use iteration than recursion; however, in certain situations recursion
                         makes more sense. Practically any loop can be converted to use recursion instead, and vice - versa.
                            So what is recursion, and how does it relate to functions? Well, in simple terms, recursion occurs when a
                          function calls itself. As you ’ d imagine, such a process would repeat indefinitely if not stopped, so the
                          recursion needs to have some sort of end condition  —  much like a loop. This end condition is known as
                          the  base case , and the part of the function that calls itself is known as the  recursive case .
                           Here ’ s a quick overview of how a recursive function operates:

                            ❑       The recursive function is called by the calling code
                            ❑       If the base case, or end condition, is met, the function does any processing required, then exits
                            ❑       Otherwise, the function does any processing required, then calls itself to continue the recursion

                            Of course, you have to make sure that the base case is eventually reached, otherwise the function will
                          keep calling itself indefinitely (causing an infinite loop).





                       Try It Out     Creating the Fibonacci Sequence with Recursion
                         Chapter 4 showed how to use looping to create the Fibonacci sequence of numbers. The following
                         script is similar to that shown in Chapter 4, except that it uses a recursive function to generate each
                         value, rather than computing the values iteratively.

                              <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                               “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
                             <html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
                               <head>
                                 <title>Fibonacci sequence using recursion</title>
                                 <link rel=”stylesheet” type=”text/css” href=”common.css” />
                                 <style type=”text/css”>
                                   th { text-align: left; background-color: #999; }
                                   th, td { padding: 0.4em; }
                                   tr.alt td { background: #ddd; }
                                 </style>
                               </head>
                               <body>
                                 <h2>Fibonacci sequence using recursion</h2>

                                 <table cellspacing=”0” border=”0” style=”width: 20em; border:
                             1px solid #666;”>
                                   <tr>
                                     <th>Sequence #</th>
                                     <th>Value</th>
                                   </tr>
                             <?php

                             $iterations = 10;

                                                                                                         161





                                                                                                      9/21/09   9:00:58 AM
          c07.indd   161
          c07.indd   161                                                                              9/21/09   9:00:58 AM
   194   195   196   197   198   199   200   201   202   203   204