What Is a Function

What Is a Function?

You can think of a function as an input/output machine. This machine takes the raw materials you feed it (input) and works with them to produce a product (output). A function accepts values, processes them, and then performs an action (printing to the browser, for example), returns a new value, or both.

آموزش کاربردی و عملی طراحی سایت اختصاصی در بسته آموزشی epishro.ir

ارایه درگاه پرداخت اینترنتی epbank.ir برای خرید و فروش و دریافت پول اینترنتی از کارت بانکی

تبلیغات اینترنتی نیازمندی شرکت خویش را میتوانید در ePishro.ir درج آگهی نمایید.

If you needed to bake a single cake, you would probably do it yourself, in your own kitchen with your standard oven. But if you needed to bake thousands of cakes, you would probably build or acquire a special cake-baking machine, built for baking cakes in massive quantities. Similarly, when deciding whether to create a function for reuse, the most important factor to consider is the extent to which it can save you from writing repetitive code.

A function is a self-contained block of code that can be called by your scripts. When called, the function’s code is executed and performs a particular task. You can pass values to a function, which then uses the values appropriately storing them, transforming them, displaying them, whatever the function is told to do. When finished, a function can also pass a value back to the original code that called it into action.

Calling Functions
Functions come in two flavors those built in to the language and those you define yourself. PHP has hundreds of built-in functions. Take a look at the following snippet for an example of a function in use:

strtoupper(“Hello Web!”);

This example calls the strtoupper() function, passing it the string “Hello Web!”. The function then goes about its business of changing the contents of the string to uppercase letters. A function call consists of the function name (strtoupper in this case) followed by parentheses. If you want to pass information to the function, you place it between these parentheses. A piece of information passed to a function in this way is called an argument. Some functions require that more than one argument be passed to them, separated by commas:

some_function($an_argument, $another_argument);

strtoupper() is typical for a function in that it returns a value. Most functions return some information back after they’ve completed their taskthey usually at least tell whether their mission was successful. strtoupper() returns a string value, so its usage requires the presence of a variable to accept the new string, such as

$new_string = strtoupper(“Hello Web!”);

You may now use $new_string in your code, such as to print it to the screen:

echo $new_string;

This code will result in the following text on the screen:


By the Way

The print() and echo() functions are not actually functions, they’re language constructs designed to output strings to the browser. However, you will find them in the PHP function list, at http://www.php.net/print and http://www.php.net/echo, respectively. These constructs are similar in functionality and can be used interchangeably. Whichever one you use is a matter of taste.

The abs() function, for example, requires a signed numeric value and returns the absolute value of that number. Let’s try it out in Listing 7.1.

Listing 7.1. Calling the Built-in abs() Function
1: <?php
2: $num = -321;
3: $newnum = abs($num);
4: echo $newnum;
5: //prints “321”
6: ?>

In this example, we assign the value -321 to a variable $num. We then pass that variable to the abs() function, which makes the necessary calculation and returns a new value. We assign this to the variable $newnum and display the result.

Put these lines into a text file called abs.php and place this file in your web server document root. When you access this script through your web browser, it produces the following:


In fact, we could have dispensed with temporary variables altogether, passing our number straight to the abs() function and directly printing the result:

echo abs(-321);

We used the temporary variables $num and $newnum, though, to make each step of the process as clear as possible. Sometimes you can make your code more readable by breaking it up into a greater number of simple expressions.

You can call user-defined functions in exactly the same way that we have been calling built-in functions.

Defining a Function
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.

Returning Values from User-Defined Functions
In the previous example, we output an amended string to the browser within the printBR() function. Sometimes, however, you will want a function to provide a value that you can work with yourself. If your function has transformed a string that you have provided, you may want to get the amended string back so that you can pass it to other functions. A function can return a value using the return statement in conjunction with a value. The return statement stops the execution of the function and sends the value back to the calling code.

Listing 7.4 creates a function that returns the sum of two numbers.

Listing 7.4. A Function That Returns a Value
1: <?php
2: function addNums($firstnum, $secondnum) {
3: $result = $firstnum + $secondnum;
4: return $result;
5: }
6: echo addNums(3,5);
7: //will print “8”
8: ?>

Put these lines into a text file called addnums.php and place this file in your web server document root. When you access this script through your web browser, it produces the following:


Notice in line 2 that addNums() should be called with two numeric arguments (line 6 shows those to be 3 and 5 in this case). These values are stored in the variables $firstnum and $secondnum. Predictably, addNums() adds the numbers contained in these variables and stores the result in a variable called $result.

The return statement can return a value or nothing at all. How we arrive at a value passed by return can vary. The value can be hard-coded:

return 4;

It can be the result of an expression:

return $a/$b;

It can be the value returned by yet another function call:

return another_function($an_argument);

Variable Scope
A variable declared within a function remains local to that function. In other words, it will not be available outside the function or within other functions. In larger projects, this can save you from accidentally overwriting the contents of a variable when you declare two variables with the same name in separate functions.

Listing 7.5 creates a variable within a function and then attempts to print it outside the function.

Listing 7.5. Variable Scope: A Variable Declared Within a Function Is Unavailable Outside the Function
1: <?php
2: function test() {
3: $testvariable = “this is a test variable”;
4: }
5: echo “test variable: “.$testvariable.”<br/>”;
6: ?>

Put these lines into a text file called scopetest.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.3.

Figure 7.3. Output of scopetest.php.

By the Way

The exact output you see depends on your PHP error settings. That is, it may or may not produce a “notice” as shown in Figure 7.3, but it will show the lack of an additional string after “test variable”.

The value of the variable $testvariable is not printed because no such variable exists outside the test() function. Remember that the attempt in line 5 to access a nonexistent variable produces a notice such as the one displayed only if your PHP settings are set to display all errors, notices, and warnings; if your error settings are not strictly set, only the string “test variable” will be shown.

Similarly, a variable declared outside a function will not automatically be available within it.

Accessing Variables with the global Statement
From within one function, you cannot (by default) access a variable defined in another function or elsewhere in the script. Within a function, if you attempt to use a variable with the same name, you will only set or access a local variable. Let’s put this to the test in Listing 7.6.

Listing 7.6. Variables Defined Outside Functions Are Inaccessible from Within a Function by Default
1: <?php
2: $life = 42;
3: function meaningOfLife() {
4: echo “The meaning of life is “.$life;
5: }
6: meaningOfLife();
7: ?>

Put these lines into a text file called scopetest2.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.4.

Figure 7.4. Attempting to reference a variable from outside the scope of a function

As you might expect, the meaningOfLife() function does not have access to the $life variable in line 2; $life is empty when the function attempts to print it. On the whole, this is a good thing because it saves us from potential clashes between identically named variables, and a function can always demand an argument if it needs information about the outside world. Occasionally, you may want to access an important variable from within a function without passing it in as an argument. This is where the global statement comes into play. Listing 7.7 uses global to restore order to the universe.

Listing 7.7. Accessing Global Variables with the global Statement
1: <?php
2: $life=42;
3: function meaningOfLife() {
4: global $life;
5: echo “The meaning of life is “.$life;
6: }
7: meaningOfLife();
8: ?>

Put these lines into a text file called scopetest3.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.5.

Figure 7.5. Successfully accessing a global variable from within a function using the global statement.

 By placing the global statement in front of the $life variable when we declare it in the meaningOfLife() function (line 4), it now refers to the $life variable declared outside the function (line 2).

You will need to use the global statement within every function that needs to access a particular named global variable. Be careful, though; if you manipulate the contents of the variable within the function, the value of the variable will be changed for the script as a whole.

You can declare more than one variable at a time with the global statement by simply separating each of the variables you want to access with commas:

global $var1, $var2, $var3;

Watch Out!

Usually, an argument is a copy of whatever value is passed by the calling code; changing it in a function has no effect beyond the function block. Changing a global variable within a function, on the other hand, changes the original and not a copy. Use the global statement carefully.

Saving State Between Function Calls with the static Statement
Local variables within functions have a short but happy lifethey come into being when the function is called and die when execution is finished, as they should. Occasionally, however, you may want to give a function a rudimentary memory.

Let’s assume that we want a function to keep track of the number of times it has been called so that numbered headings can be created by a script. We could, of course, use the global statement to do this, as shown in Listing 7.8.

Listing 7.8. Using the global Statement to Remember the Value of a Variable Between Function Calls
1: <?php
2: $num_of_calls = 0;
3: function numberedHeading($txt) {
4: global $num_of_calls;
5: $num_of_calls++;
6: echo “<h1>”.$num_of_calls.” “.$txt.”</h1>”;
7: }
8: numberedHeading(“Widgets”);
9: echo “<p>We build a fine range of widgets.</p>”;
10: numberedHeading(“Doodads”);
11: echo “<p>Finest in the world.</p>”;
12: ?>

Put these lines into a text file called numberedheading.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.6.

Figure 7.6. Using the global statement to keep track of the number of times a function has been called.

This does the job. We declare a variable, $num_of_calls, in line 2, outside the function numberedHeading(). We make this variable available to the function using the global statement in line 4.

Every time numberedHeading() is called, the value of $num_of_calls is incremented (line 5). We can then print out the heading complete with the properly incremented heading number.

This is not the most elegant solution, however. Functions that use the global statement cannot be read as standalone blocks of code. In reading or reusing them, we need to look out for the global variables that they manipulate.

This is where the static statement can be useful. If you declare a variable within a function in conjunction with the static statement, the variable remains local to the function, and the function “remembers” the value of the variable from execution to execution. Listing 7.9 adapts the code from Listing 7.8 to use the static statement.

Listing 7.9. Using the static Statement to Remember the Value of a Variable Between Function Calls
1: <?php
2: function numberedHeading($txt) {
3: static $num_of_calls = 0;
4: $num_of_calls++;
5: echo “<h1>”.$num_of_calls.” “. $txt.”</h1>”;6: }
7: numberedHeading(“Widgets”);
8: echo “<p>We build a fine range of widgets.</p>”;
9: numberedHeading(“Doodads”);
10: echo “<p>Finest in the world.</p>”;
11: ?>

The numberedHeading() function has become entirely self-contained. When we declare the $num_of_calls variable on line 3, we assign an initial value to it. This assignment is made when the function is first called on line 7. This initial assignment is ignored when the function is called a second time on line 9. Instead, the code remembers the previous value of $num_of_calls. We can now paste the numberedHeading() function into other scripts without worrying about global variables. Although the output of Listing 7.9 is exactly the same as that of Listing 7.8, we have made the code a bit more elegant.

More About Arguments
You’ve already seen how to pass arguments to functions, but there’s plenty more to cover. In this section, you’ll look at a technique for giving your arguments default values and explore a method of passing variables by reference rather than by value. This means that the function is given an alias of the original value rather than a copy of it.

Setting Default Values for Arguments
PHP provides a nifty feature to help build flexible functions. Until now, we’ve said that some functions require one or more arguments. By making some arguments optional, you can render your functions a little less autocratic.

Listing 7.10 creates a useful little function that wraps a string in an HTML span element. We want to give the user of the function the chance to change the font-size style, so we demand a $fontsize argument in addition to the string (line 2).

Listing 7.10. A Function Requiring Two Arguments
1: <?php
2: function fontWrap($txt, $fontsize) {
3: echo “<span style=\”font-size:$fontsize\”>”.$txt.”</span>”;
4: }
5: fontWrap(“A Heading<br/>”,”24pt”);
6: fontWrap(“some body text<br/>”,”16pt”);
7: fontWrap(“smaller body text<br/>”,”12pt”);
8: fontWrap(“even smaller body text<br/>”,”10pt”);
9: ?>

Put these lines into a text file called fontwrap.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.7.

Figure 7.7. A function that formats and outputs strings.

By assigning a value to an argument variable within the function definition’s parentheses, we can make the $fontsize argument optional. If the function call doesn’t define an argument for this argument, the value we have assigned to the argument is used instead. Listing 7.11 uses this technique to make the $fontsize argument optional.

Listing 7.11. A Function with an Optional Argument
1: <?php
2: function fontWrap($txt, $fontsize = “12pt”) {
3: echo “<span style=\”font-size:$fontsize\”>”.$txt.”</span>”;
4: }
5: fontWrap(“A Heading<br/>”,”24pt”);
6: fontWrap(“some body text<br/>”);
7: fontWrap(“smaller body text<br/>”);
8: fontWrap(“even smaller body text<br/>”);
9: ?>

When the fontWrap() function is called with a second argument, as in line 5, this value is used to set the font-size attribute of the span element. When we omit this argument, as in lines 6, 7, and 8, the default value of “12pt” is used instead. You can create as many optional arguments as you want, but when you’ve given an argument a default value, all subsequent arguments should also be given defaults.

Passing Variable References to Functions
When you pass arguments to functions, they are stored as copies in parameter variables. Any changes made to these variables in the body of the function are local to that function and are not reflected beyond it. This is illustrated in Listing 7.12.

Listing 7.12. Passing an Argument to a Function by Value
1: <?php
2: function addFive($num) {
3: $num += 5;
4: }
5: $orignum = 10;
6: addFive($orignum);
7: echo $orignum;
8: ?>

Put these lines into a text file called addfive.php and place this file in your web server document root. When you access this script through your web browser, it produces the following:


The addFive() function accepts a single numeric value and adds 5 to it, but it returns nothing. We assign a value to a variable $orignum in line 5 and then pass this variable to addFive() in line 6. A copy of the contents of $orignum is stored in the variable $num. Although we increment $num by 5, this has no effect on the value of $orignum. When we print $orignum, we find that its value is still 10. By default, variables passed to functions are passed by value. In other words, local copies of the values of the variables are made.

We can change this behavior by creating a reference to our original variable. You can think of a reference as a signpost that points to a variable. In working with the reference, you are manipulating the value to which it points.

Listing 7.13 shows this technique in action. When you pass an argument to a function by reference, as in line 6, the contents of the variable you pass ($orignum) are accessed by the argument variable and manipulated within the function, rather than just a copy of the variable’s value (10). Any changes made to an argument in these cases will change the value of the original variable. You can pass an argument by reference by adding an ampersand to the argument name in the function definition, as shown in line 2.

Listing 7.13. Using a Function Definition to Pass an Argument to a Function by Reference
1: <?php
2: function addFive(&$num) {
3: $num += 5;
4: }
5: $orignum = 10;
6: addFive($orignum);
7: echo $orignum;
8: ?>

Put these lines into a text file called addfive2.php and place this file in your web server document root. When you access this script through your web browser, it produces the following:


Testing for the Existence of a Function
We do not always know that a function exists before we try to invoke it. Different builds of the PHP engine may include different functionality, and if you are writing a script that may be run on multiple servers, you might want to verify that key features are available. For instance, you might want to write code that will use MySQL if MySQL-related functions are available but simply log data to a text file otherwise.

You can use function_exists() to check for the availability of a function. function_exists() requires a string representing a function name. It returns true if the function can be located and false otherwise.

Listing 7.14 shows function_exists() in action and illustrates some of the other topics we have covered in this chapter.

Listing 7.14. Testing for a Function’s Existence
1: <?php
2: function tagWrap($tag, $txt, $func = “”) {
3: if ((!empty($txt)) && (function_exists($func))) {
4: $txt = $func($txt);
5: return “<“.$tag.”>”.$txt.”</”.$tag.”><br/>”;
6: } else {
7: return “<b>”.$txt.”</b><br/>”;
8: }
9: }
11: function underline($txt) {
12: return “<span style=\”text-decoration:underline;\”>”.$txt.”</span>”;
13: }
15: echo tagWrap(‘b’, ‘make me bold’);
17: echo tagWrap(‘i’, ‘underline me too’, “underline”);
19: echo tagWrap(‘i’, ‘make me italic and quote me’,
20: create_function(‘$txt’, ‘return “&quot;$txt&quot;”;’));
21: ?>

We define two functions, tagWrap() (line 2) and underline() (line 11). The tagWrap() function accepts three strings: a tag, the text to be formatted, and an optional function name. It returns a formatted string. The underline() function requires a single argumentthe text to be formattedand returns the text wrapped in <span> tags with appropriate style attributes.

When we first call tagWrap() on line 15, we pass it the character b and the string “make me bold”. Because we haven’t passed a value for the function argument, the default value (an empty string) is used. On line 3, we check whether the $func variable contains characters, and, if it is not empty, we call function_exists() to check for a function by that name. Of course, in this case, the $func variable is empty, so we wrap the $txt variable in <b> tags in the else clause on lines 67 and return the result.

We call tagWrap() on line 17 with the string ‘i’, some text, and a third argument: “underline”. function_exists() finds a function called underline() (line 11), so it calls this function and passes the $txt argument variable to it before any further formatting is done. The result is an italicized, underlined string.

Finally, on line 19, we call tagWrap(), which wraps text in quotation entities. It would be quicker to simply add the entities to the text to be transformed ourselves, but this example serves to illustrate the point that function_exists() works as well on anonymous functions as it does on strings representing function names.

Put these lines into a text file called exists.php and place this file in your web server document root.