13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

144 Chapter 5 Reusing Code <strong>and</strong> Writing Functions<br />

In this case, the parameter used is a string containing only the word parameter, but the<br />

following calls may also be fine depending on what parameters the function expects:<br />

function_name(2);<br />

function_name(7.993);<br />

function_name($variable);<br />

In the last line, $variable might be any type of <strong>PHP</strong> variable, including an array or<br />

object.<br />

A parameter can be any type of data, but particular functions usually require particular<br />

data types.<br />

You can see how many parameters a function takes, what each represents, <strong>and</strong> what<br />

data type each needs to be from the function’s prototype.We often show the prototype in<br />

this book when we describe a function.<br />

This is the prototype for the function fopen():<br />

resource fopen ( string filename, string mode<br />

[, bool use_include_path [, resource context]])<br />

The prototype tells you a number of things, <strong>and</strong> it is important that you know how to<br />

correctly interpret these specifications. In this case, the word resource before the function<br />

name tells you that this function will return a resource (that is, an open file h<strong>and</strong>le).<br />

The function parameters are inside the parentheses. In the case of fopen(), four parameters<br />

are shown in the prototype.The parameters filename <strong>and</strong> mode are strings, the<br />

parameter use_include_path is a Boolean, <strong>and</strong> the parameter context is a resource.The<br />

square brackets around use_include_path <strong>and</strong> context indicate that these parameters<br />

are optional.You can provide values for optional parameters, or you can choose to ignore<br />

them <strong>and</strong> the default value will be used. Note, however, that for a function with more<br />

than one optional parameter, you can only leave out parameters from the right. For<br />

example, when using fopen(), you can leave out context or you can leave out both<br />

use_include_path <strong>and</strong> context; however, you cannot leave out use_include_path but<br />

provide context.<br />

After reading the prototype for this function, you know that the following code fragment<br />

is a valid call to fopen():<br />

$name = ‘myfile.txt’;<br />

$openmode = ‘r’;<br />

$fp = fopen($name, $openmode);<br />

This code calls the function named fopen().The value returned by the function will be<br />

stored in the variable $fp. For this example, we chose to pass to the function a variable<br />

called $name containing a string representing the file we want to open <strong>and</strong> a variable<br />

called $openmode containing a string representing the mode in which we want to open<br />

the file.We chose not to provide the optional third <strong>and</strong> fourth parameters.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!