Specifications
Most functions do require one or more parameters—information given to a function when it is
called that influences the outcome of executing the function. We pass parameters by placing
the data or the name of a variable holding the data inside parentheses after the function name.
A call to a function with a parameter resembles the following:
function_name(“parameter”);
In this case, the parameter we used was a string containing only the word parameter, but the
following calls are also fine depending on the function:
function_name(2);
function_name(7.993);
function_name($variable);
In the last line, $variable might be any type of PHP variable, including an array.
A parameter can be any type of data, but particular functions will usually require particular
data types.
You can see how many parameters a function takes, what each represents, and what data type
each needs to be from the function’s prototype. We often show the prototype when we describe
a function, but you can find a complete set of function prototypes for the PHP function library
at http://www.php.net.
This is the prototype for the function fopen():
int fopen( string filename, string mode, [int use_include_path] );
The prototype tells us a number of things, and it is important that you know how to correctly
interpret these specifications. In this case, the word int before the function name tells us that
this function will return an integer. The function parameters are inside the parentheses. In the
case of fopen(), three parameters are shown in the prototype. The parameter filename and
mode are strings and the parameter is an integer.
The square brackets around use_include_path indicate that this parameter is optional. We can
provide values for optional parameters or we can choose to ignore them, and the default value
will be used.
After reading the prototype for this function, we know that the following code fragment will be
a valid call to fopen():
$name = “myfile.txt”;
$openmode = “r”;
$fp = fopen($name, $openmode)
This code calls the function named fopen(). The value returned by the function will be stored
in the variable $fp. We chose to pass to the function a variable called $name containing a string
Using PHP
P
ART I
130
07 7842 CH05 3/6/01 3:35 PM Page 130