Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)

You can define your own functions 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.

By the Way

The naming rules for functions are similar to the naming rules for variables, which you learned in Chapter 5, "The Building Blocks of PHP." Names cannot include spaces, and they must begin with a letter or an underscore. As with variables, your function names should be meaningful as well as consistent in style. The capitalization of function names is one such stylistic touch you can add to your code; using mixed case in names, such as myFunction() or handleSomeDifficultTask(), makes your code much easier to read.

Listing 7.2 declares and calls a function.

Listing 7.2. Declaring and Calling a Function

1: <?php 2: function bighello() { 3: echo "<h1>HELLO!</h1>"; 4: } 5: bighello(); 6: ?>

The script in Listing 7.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 7.1.

Figure 7.1. Output of bighello.php.

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

Listing 7.3. Declaring a Function That Requires an Argument

1: <?php 2: function printBR($txt) { 3: echo $txt."<br/>"; 4: } 5: printBR("This is a line."); 6: printBR("This is a new line."); 7: printBR("This is yet another line."); 8: ?>

By the Way

Unlike variable names, function names are not case sensitive. In the example preceding, the printBR() function could have been called printbr(), PRINTBR(), or any combination thereof, with success.

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 7.2.

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

In line 2, 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 this $txt variable. Within the body of the function, in line 3, we print the $txt variable, appending a <br/> element to it.

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

Категории