In this chapter,
you??™ll learn all about PHP functions, including how to create and invoke them, pass
input to them, return both single and multiple values to the caller, and create and
include function libraries. Additionally, you??™ll learn about both recursive and variable
functions.
Invoking a Function
More than 1,000 functions are built into the standard PHP distribution, many of which
you??™ll see throughout this book. You can invoke the function you want simply by specifying
the function name, assuming that the function has been made available either
114 CHAPTER 4 ?– F UNCT I ONS
through the library??™s compilation into the installed distribution or via the include()
or require() statement. For example, suppose you want to raise five to the third
power. You could invoke PHP??™s pow() function like this:
$value = pow(5,3); // returns 125
echo $value;
?>
If you want to output the function results, you can bypass assigning the value to a
variable, like this:
echo pow(5,3);
?>
If you want to output the function outcome within a larger string, you need to concatenate
it like this:
echo "Five raised to the third power equals ".pow(5,3).".";
Or perhaps more eloquently, you could use printf():
printf("Five raised to the third power equals %d.", pow(5,3));
In either case, the following output is returned:
Five raised to the third power equals 125.
Pages:
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198