Page 193 - Beginning PHP 5.3
P. 193

Chapter 7: Functions
                            ❑       To create functions dynamically  —  You can customize the code within an anonymous function
                                at the time that you create it. Although you ’ ll rarely need to do this, it can make your code very
                                flexible in certain specific situations
                            ❑     To create short - term, disposable functions  —  Commonly, you do this when you work with built - in
                                functions that require you to create a callback or event handler function to work with. Examples
                                include   xml_set_element_handler() , which you meet in Chapter  19 , and array functions such
                                as   array_walk() , which lets you apply a function to each value in an array, and  usort() , which
                                sorts an array ’ s elements according to a comparison function that you create yourself

                           To create an anonymous function, you use   create_function() . This expects two arguments: a comma -
                            separated list of parameters (if any), and the code for the function body (minus the curly brackets, but
                          including a semicolon at the end of each line of code). It returns a unique, randomly generated string
                          value that you can use to refer to the function later:

                             $myFunction = create_function( ‘$param1, $param2’, ‘function code here;’ );

                          Here ’ s a trivial example that creates an anonymous function dynamically based on the value of a variable:
                             $mode = “+”;
                             $processNumbers = create_function( ‘$a, $b’, “return \$a $mode \$b;” );
                             echo $processNumbers( 2, 3 ); // Displays “5”

                           This code uses the value of the  $mode  variable as the operator used to process its two arguments,  $a  and
                           $b . For example, if you change  $mode  to   “ * ” ,  the code displays   “ 6 ”    instead (2 times 3). In itself this code

                          is rather pointless, but if you can imagine a more complex function, where its contents are determined by
                          external factors such as user input, you can see that it ’ s a potentially powerful feature of PHP.
                            More commonly, you ’ ll use anonymous functions to create callback functions as required by certain
                          built - in functions, as shown in the following example.

                       Try It Out     Sorting Words by Length
                         This example script takes a block of text and sorts all the words within the text by the number of
                         letters in each word, shortest word first. To do this, it uses anonymous functions, along with PHP’s
                         built-in usort(), preg_replace(), array_unique(), and preg_split() functions.
                         Save the script as sort_words_by_length.php in your document root folder, then run it in your
                         browser. You should see a page similar to Figure 7-5.
                             <!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>Sorting words in a block of text by length</title>
                                 <link rel=”stylesheet” type=”text/css” href=”common.css” />
                               </head>
                               <body>
                                 <h1>Sorting words in a block of text by length</h1>
                             <?php

                             $myText = <<<END_TEXT
                                                                                                         155





          c07.indd   155                                                                              9/21/09   9:00:56 AM
          c07.indd   155
                                                                                                      9/21/09   9:00:56 AM
   188   189   190   191   192   193   194   195   196   197   198