Page 198 - Beginning PHP 5.3
P. 198

Part II: Learning the Language
                  Returning References from Your Own Functions

                   As well as passing variables by reference into functions, you can also get functions to return references,
                 rather than values. To do this, you place an ampersand before the function name in your function
                 definition. Then, when you return a variable with the   return  statement, you pass a reference to that
                variable back to the calling code, rather than the variable ’ s value:

                    function  & myFunc(){
                      // (do stuff)
                      return $var;  // Returns a reference to $var

                    }
                  Here ’ s an example that shows return - by - reference in action:

                    $myNumber = 5;
                    function  & getMyNumber() {
                      global $myNumber;
                      return $myNumber;
                    }

                    $numberRef = &  getMyNumber();
                    $numberRef++;
                    echo “\$myNumber = $myNumber < br/ > ”;   // Displays “6”
                    echo “\$numberRef = $numberRef < br/ > ”; // Displays “6”

                   Here ’ s how it works. First, a global variable,  $myNumber , is created and given the value  5 . Next, a
                 function,   getMyNumber() , is defined. This function simply uses the  global  keyword to access the
                 global variable   $myNumber , then returns  $myNumber . Because  getMyNumber()  has an ampersand before
                 its name, it returns a reference to   $myNumber , rather than the value that  $myNumber  holds.

                  Next, the script calls   getMyNumber() . The return value of  getMyNumber()  —  that is, the reference to


                  $myNumber  —  is then assigned to a new variable,  $numberRef . Notice the ampersand after the equals
                 sign; this ensures that   $numberRef  takes on the reference returned by  getMyNumber() , rather than
                 taking on the value that the reference points to.
                   At this point,   $numberRef  and  $myNumber  both point to the same contents. To prove this, the code
                 increments   $numberRef , then outputs the values of both  $myNumber  and  $numberRef . Notice that they

                both hold the same value  —   6  —  because they both point to the same piece of data.

                   Return - by - reference is used quite often in languages such as C++, because it ’ s the easiest way to return
                 complex data structures such as arrays and objects. However, because PHP lets you return pretty much
                 anything with its   return  statement, and automatically returns objects by reference anyway (as you see
                in the next chapter), you probably won ’ t use return - by - reference that much in PHP.



                  Writing Recursive Functions

                   In Chapter  4 , you learned how to use loops to operate on large amounts of data at once. Looping is
                 often known as  iteration .


              160





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