Page 196 - Beginning PHP 5.3
P. 196

Part II: Learning the Language
                   Finally, the script displays the sorted list of words in another fixed - width  div  element.

                  By the way, you don ’ t have to use an anonymous function in this situation. The preceding line of code
                could be written as:

                    function sortByLength( $a, $b ) {
                      return strlen( $a ) - strlen( $b );
                    }

                    usort( $words, “sortByLength” );

                   As you can see, though, the anonymous function version is much more compact.
                  Working with References

                   You ’ ve already learned that you can pass information to a function in the form of arguments, as well as
                 return information from a function to its calling code using the   return  statement. When you do either of
                these things, PHP actually passes copies of the information to and from the function; this is known as
                passing and returning  by value .
                  Most of the time this isn ’ t a problem, but sometimes you want your function to work on the original
                information, rather than on a copy. Consider the following example:
                    function resetCounter( $c ) {
                      $c = 0;
                    }

                    $counter = 0;
                    $counter++;
                    $counter++;
                    $counter++;
                    echo “$counter < br/ > ”;  // Displays “3”
                    resetCounter( $counter );

                    echo “$counter < br/ > ”;  // Displays “3”
                   This code defines a function,  resetCounter() , that resets its argument to zero. A  $counter  variable is
                then initialized to zero and incremented three times. As you ’ d expect, the value of   $counter  at this
                 point is   3 .  resetCounter()  is then called, passing in  $counter , the variable to reset. However, as the
                second   echo  statement shows,  $counter  has not been reset by the function. This is because the
                parameter   $c  inside  resetCounter()  merely holds a copy of the information stored in  $counter . So
                when the function sets   $c  to zero, it doesn ’ t affect the value of  $counter  at all.

                   Fortunately, PHP provides a mechanism known as  references  that you can use to work around such issues.
                 A reference is a bit like a shortcut or alias to a file on your hard drive. When you create a reference to a
                 PHP variable, you now have two ways to read or change the variable ’ s contents  —  you can use the
                 variable name, or you can use the reference. Here ’ s a simple example that creates a reference to a variable:
                    $myVar = 123;
                    $myRef = &  $myVar;
                    $myRef++;
                    echo $myRef . “ < br/ > ”;  // Displays “124”

                    echo $myVar . “ < br/ > ”;  // Displays “124”
              158





                                                                                                      9/21/09   9:00:57 AM
          c07.indd   158
          c07.indd   158                                                                              9/21/09   9:00:57 AM
   191   192   193   194   195   196   197   198   199   200   201