Passing Data to Functions


You can also send data to a function by using its argument list, which is a comma-separated list of variable names:

 function function_name([argument_list...]) {     [statements;]     [return return_value;] } 

How does this work? Say for example that you wanted to customize the navigation bar we created in the previous chunk by adding some text and a copyright statement. You could do that by passing those two items to the nav_bar function. If you wanted to refer to those items as, say, $text and $copyright in the body of the function, you could set up the function like this:

 function nav_bar($text, $copyright) {     .     .     . } 

Now you can refer to the data passed to the function by name, as $text and $copyright. Here's how we add their values to the navigation bar, displaying it in italics:

 function nav_bar($text, $copyright) {     echo "<hr>";     echo "<center>";     echo "<a href='home.html'>Home</a>&nbsp;&nbsp;&nbsp;";     echo "<a href='map.html'>Site Map</a>&nbsp;&nbsp;&nbsp;";     echo "<a href='help.html'>Help</a>";     echo "<hr>";     echo "<FONT SIZE='1'><I>$text</I></FONT>";     echo "<BR><FONT SIZE='1'><I>$copyright</I></FONT>";     echo "</center>"; } 

How do you call this new version of the function? Take a look at phpcustomnavbar.php, Example 4-2, where we're passing the text "SuperDuperBig Co." and "(c) 2005" to this function; these text items will be stored in the variables $text and $copyright, respectively.

Example 4-2. Passing data to a function, phpcustomnavbar.php
 <HTML>         <HEAD>             <TITLE>Passing data to customize a navigation bar</TITLE>         </HEAD>         <BODY>             <H1>Passing data to customize a navigation bar</H1>             <?php                 echo "<H3>Welcome to my web page!</H3>";                 echo "<br>";                 echo "How do you like it?";                 echo "<br><br>";                 nav_bar("SuperDuperBig Co.", "(c) 2005");                 function nav_bar($text, $copyright)                 {                     echo "<hr>";                     echo "<center>";                     echo "<a href='home.html'>Home</a>&nbsp;&nbsp;&nbsp;";                     echo "<a href='map.html'>Site Map</a>&nbsp;&nbsp;&nbsp;";                     echo "<a href='help.html'>Help</a>";                     echo "<hr>";                     echo "<FONT SIZE='3'><I>$text</I></FONT>";                     echo "<BR><FONT SIZE='3'><I>$copyright</I></FONT>";                     echo "</center>";                 }             ?>     </BODY> </HTML> 

You can see the results in Figure 4-2, where the nav_bar function has been able to read the data we passed to it and has inserted it into the navigation bar.

Figure 4-2. Passing data to a function.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net