Defining a Function

You can define a function using the function statement:

 function some_function( $argument1, $argument2 ) {      // function code here } 

The name of the function follows the function statement and precedes a set of parentheses. If your function requires arguments, you must place comma-separated variable names within the parentheses. These variables will be filled by the values passed to your function. Even if your function doesn't require arguments, you must nevertheless supply the parentheses.

graphics/book.gif

The naming rules for functions are similar to the naming rules for variables, which you learned in Hour 4, "The Building Blocks of PHP." Names cannot include spaces, and they must begin with a letter or an underscore.


Listing 6.2 declares a function.

Listing 6.2 Declaring a Function
   1: <html>   2: <head>   3: <title>Listing 6.2</title>   4: </head>   5: <body>   6: <?php   7: function bighello() {   8:      print "<h1>HELLO!</h1>";   9: }  10: bighello();  11: ?>  12: </body>  13: </html> 

The script in Listing 6.2 simply outputs the string "HELLO" wrapped in an HTML <h1> element.

Put these lines into a text file called bighello.php, and place this file in your Web server document root. When you access this script through your Web browser, it should look like Figure 6.1.

Figure 6.1. Output of Listing 6.2.

graphics/06fig01.gif

We declare a function bighello() that requires no arguments. Because of this, we leave the parentheses empty. bighello() is a working function but is not terribly useful. Listing 6.3 creates a function that requires an argument and actually does something helpful with it.

Listing 6.3 Declaring a Function That Requires Arguments
   1: <html>   2: <head>   3: <title>Listing 6.3</title>   4: </head>   5: <body>   6: <?php   7: function printBR( $txt ) {   8:      print ("$txt<br>\n");   9: }  10: printBR("This is a line");  11: printBR("This is a new line");  12: printBR("This is yet another line");  13: ?>  14: </body>  15: </html> 

Put these lines into a text file called printbr.php, and place this file in your Web server document root. When you access this script through your Web browser, it should look like Figure 6.2.

Figure 6.2. A function that prints a string with an appended <br> tag.

graphics/06fig02.gif

In line 7, the printBR() function expects a string, so we place the variable name $txt between the parentheses when we declare the function. Whatever is passed to printBR() will be stored in $txt. Within the body of the function, in line 8, we print the $txt variable, appending a <br> element and a newline character to it.

When we want to write a line to the browser, such as in line 10, 11, or 12, we can call printBR() instead of the built-in print(), saving us the bother of typing the <br> element.



Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
ISBN: 067232489X
EAN: 2147483647
Year: 2005
Pages: 263

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