Page 181 - Beginning PHP 5.3
P. 181

Chapter 7: Functions
                           If a function returns a value, you can assign the value to a variable:

                               $returnVal = functionName( argument );

                           You can also pass the return value directly to another function, such as  print() :

                               print( functionName( argument ) );

                           In general terms, the return value of a function call is an expression, which means you can use a
                         function ’ s return value anywhere that you can use an expression.

                           When you call a function from within your script, the PHP engine jumps to the start of that function and
                         begins running the code inside it. When the function is finished, the engine jumps back to the point just
                         after the code that called the function and carries on from there. Here ’ s a simple example that illustrates
                         this point:

                                 < !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 > Square roots < /title >
                                  < link rel=”stylesheet” type=”text/css” href=”common.css” / >
                                < /head >
                                < body >
                                  < h1 > Square roots < /h1 >
                               < ?php

                             echo “The square root of 9 is: “ . sqrt( 9 ) . “. < br/ > ”;
                             echo “All done! < br/ > ”;
                             ? >
                                < /body >
                               < /html >
                           This code produces the output shown in Figure  7 - 1 . Here ’ s how it works:

                            ❑       After displaying the XHTML page header, the first   echo()  line is run, and the PHP engine
                                evaluates the expression after the   echo()  statement. This includes a function call to PHP ’ s built -
                                 in   sqrt()  function, which determines the square root of its argument (in this case,  9 )
                            ❑       The engine jumps to the code for the   sqrt()  function and runs it. The function does its job and
                                exits, returning the value   3
                            ❑       The engine jumps back to the first   echo()  statement and, now that it knows the result of the call
                                to   sqrt() , evaluates the rest of the expression, producing the string:   “ The square root of 9
                                         This string value is then displayed in the Web page using the  echo()  statement
                                is: 3. ”
                            ❑       Finally, the engine moves to the next line of code, and displays the    “ All done! ”    message








                                                                                                         143





                                                                                                      9/21/09   9:00:51 AM
          c07.indd   143
          c07.indd   143                                                                              9/21/09   9:00:51 AM
   176   177   178   179   180   181   182   183   184   185   186