User-defined functions provide a way to group together related statements into a cohesive block. For reusable code, a function saves duplicating statements and makes maintenance of the code easier. Consider an example of a simple user-developed function as shown in Example 2-3.
Example 2-3. A user-defined function to output bold text
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
The script defines the function bold( ), which takes one parameter,$string, and prints that string prefixed by a bold<b>tag and suffixed with a</b>tag. The parameter$stringis a variable that is available in the body of the function, and the value of$stringis set when the function is called. As shown in the example, the function can be called with a string literal expression or a variable as the parameter.
Functions can also return values. For example, consider the following code fragment that declares and uses a function heading( ), which returns a string using thereturnstatement:
function heading($text, $headingLevel)
The function takes two parameters: the text of a heading and a heading level. Based on the value of$headingLevel, the function builds the HTML suitable to display the heading. The example outputs the string:
The variable that is returned by areturn statement can optionally be placed in parentheses: the statementsreturn($result)andreturn $resultare identical.Parameter Types and Return Types
The parameter and return types of a function aren’t declared when the function is defined. PHP allows parameters of any type to be passed to the function, and as with variables, the return type is determined when a result is actually returned. Consider a simple function that divides two numbers:
function divide($a, $b)
The value returned from the function divide( ) is the value of the expression($a/$b). The type that is returned depends on the parameters passed to divide( ). For example:
$c = divide(4, 2); // assigns an integer value = 2
If the types of parameters passed to the function are critical, they should be tested as shown earlier in “Type Conversion.”
Thereturnstatement causes the execution of the function to end. To illustrate this, consider an improved divide( ) function definition that tests the parameter$bto avoid divide-by-zero errors:
function divide($a, $b)
If$bis 0, then the function returnsfalseand the division of$a/$bis never executed.
Thereturnstatement can also be used to exit from functions that don’t return values. Consider the following definition of bold( ) that simply prints the parameter$stringwithout any bold mark-up when passed non-string values:
Variables used inside a function are different from those used outside a function. The variables used inside the function are limited to use within the function. This is called the scope of the variable. There are exceptions to this rule, which are discussed later in this section. Consider an example that illustrates variable scope:
This example outputs the string:
with no value for$temp. The scope of the variable$tempis local to the function doublevalue( ) and is discarded when the function returns.
The PHP script engine doesn’t complain about an undeclared variable being used. It just assumes the variable is empty. However, this use of an undefined variable can be detected by configuring the error-reporting settings. Error reporting is discussed in Chapter 14.
The easiest way to use a value that is local to a function elsewhere in a script is to return the value from the function with thereturnstatement. The calling script can simply assign the returned value to a local variable. The following example does this:
The example prints:
$temp is: 10
You could have still used the variable name$tempinside the function doublevalue( ). However, the$tempinside the function is a different variable from the$tempoutside the function. The general rule is that variables used exclusively within functions are local to the function, regardless of whether an identically named variable is used elsewhere. There are three exceptions to this general rule: variables passed by reference, variables declaredglobalin the function, and superglobals that contain user and environment values and are automatically created by PHP at runtime. Global variables are discussed in the next section, and superglobals are discussed in Chapter 6.Global variables
If you want to use the same variable everywhere in your code, including within functions, you can do so with the global statement. The global statement declares a variable within a function as being the same as the variable that is used outside of the function. Consider this example:
Because$tempis declared inside the function asglobal, the variable$tempused in doublevalue( ) is a global variable that can be accessed outside the function. Because the variable$tempcan be seen outside the function, the script prints:
$temp is: 10
A word of caution: avoid overuse ofglobalas it makes for confusing code.
Allowing a function to modify global variables solves the problem that areturnstatement can only pass back one value. An alternative to usingglobalis to return an array of values—this approach becomes clear when we discuss arrays in Chapter 3. A better approach is to pass parameters by reference instead of by value, a practice described later.
blog comments powered by Disqus