Dr. Logan | COM Courses | Syllabus | Reading Notes

Reusable Code: Well-written, debugged code that is used on many pages saves you development time. It's your life-blood, in fact. PHP does better than server-side includes to facilitate reusing code. The goal is to save time and money, to have more reliable code, and to be consistent within a site.

PHP file inclusions: Loading files into your script is done with statements require() and include(). Both use files stored on the server. Files are handled according to file extension (just use .php; see cautions on non-standard extensions such as .inc, page 132.). When loaded, the file becomes part of the PHP file and is executed as though it were PHP; the code in each include file should be embedded in (i.e., the file should begina and end with) <?php and ?> tags.

Use require() to embed elements that are repeated across many (or all) pages within a site. This usually includes navigation, banner, and footer items, for example. Then, no matter how many pages will use these embedded files, you only need to change a single file to make perhaps hundreds of page changes. The statement include() is identical to require() except in that when they fail, require() produces a fatal error but include() only issues a warning. Statements require_once() and include_once() include a check to see whether a file has been used; this prevents multiple inclusions of pieces of files that may be replicated in multiple file libraries; duplicating files might lead to redefining functions or other things that cause errors.

Functions

Calling: Good coding practice in all computer languages involves isolating programming from content as much as possible; the W3 encourages such practice for web programming as well. Code modularity comes from placing repeated code (or code that has been developed and tested separately and reused on a particular page) in a separate part the page as a PHP function. Where the code is needed on the page, it is called by simply naming the function followed by parentheses, as in myfunction(). For example, the built-in function phpinfo() tests the php engine by displaying information about PHP, the web server setup, and values of various PHP and server variables; the call is simply phpinfo();. If the function needs information, you pass parameters to it within the parentheses, either as literals (numbers, strings) or by reference to a variable (scalar or array). Parameters may be restricted to certain types, depending on how the function is written; it may be looking for a string, an integer, or an array, for example.

If you call a function that doesn't exist, you'll get an error message. Either you spelled incorrectly, or if you are using a built-in function, perhaps your version of PHP doesn't have it. PHP5 added many functions that won't work if your server is only running the PHP4 engine (which is still common).

Prototypes: When you look at the description of a function, you'll see a prototype. The example on page 140 is for the function fopen(), which has the prototype resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]]). This means that fopen will return a resource (here, an open file handle that let's me use a file). The function needs some things, and these include 2 required and 2 optional parameters: filename and mode are strings and are required; use_include_path and zcontext, the parameters inside the square brackets, are optional, meaning that you can provide them but if you don't the function has some built-in default values that it will use instead. If you omit parameters, you can only do so from the right.

User-built Functions

Custom Functions: Building your own functions starts with declaration. This identifies, names, lists parameters, and contains code. Syntax is
function my_function ( parameters list, if any )
{
code to be called
}

Function names should be descriptive; they may include letters, numbers, or underscores but may not begin with a digit; and, they should not be re-used or take the names of any built-in PHP function. Function names are not case sensitive, unlike variable names.

Functions are calledby name—my_function( parameters, if any );./span>. Parameters may be specified in the function; optional parameters are named within the parentheses and given a default value—function my_function (first_answer = 'no'...). When calling a function with optional parameters, you can therefore omit them if you feel the default value will work. If you have a list of optional parameters, however, you can't leave out one that comes first in the list. See page 146 for functions that accept a variable number of parameters; functions func_num_args() returns the number of arguments in the function, func_get_args() packs them into an array, and func_get_arg() lets you access members of the array.

Scope. The scope of a variable is an attribute that determines where it can be used; alternatively, this can be described as where it is visible. PHP's scope rules are

See examples on pages 147-149.

Passing by Reference or by Value: Given scope rules, it is possible to have variables with identical names but different values, one determined by scope outside of a function and the other local to the function. They are different because of scope, so that changes to the variable within the function will not affect its value outside of the function! Placing a variable in a call to the function, that is, merely passes its value, appropriately referred to as a pass by value. The alternative, passing by reference, creates a pointer to the variable that is passed into the function; when the value is changed within the function, the value of the variable outside of the function is also changed. This is done easily by simply putting "&" in front of the variable name in the function declaration, as function my_function (&$my_referenced_variable...)

Return: Used inside a function, return stops any further execution of the function and returns execution to the statement following the function call. It is used with conditional branching structures. Any code inside the function after the return is ignored.

If you include a variable on the line with a return, as in return $my_answer, the value of the variable will be available to the calling program, as in $what_is = my_oracle(), which would place the value contained in $my_answer into the variable $what_is.

Recursive Functions

Recursion: If a function can call itself, it is a recursion. I once wrote a program that had pairs of possible key descriptions used to indentify dragonflies. Each pair required me to decide whether the insect under my microscope had or did not have the characters described (does the insect have wings or not, for example). Each pair came from a database, and for each option there was a record of the previous options that had sent me to the pair as well as a pointer to the next pair (it has no wings so let's ask a question about insects with no wings next...). After I found my insect (wings, long feet, no bumps on its sides...), I wanted to go back to construct the entire chain of keys to indicate the path I'd followed. I used function that stepped backwards, calling the function over and over again for each step I'd previously taken, but not writing anything until I got back to the very beginning, and then writing the next, and the next, etc. This was recursion. You don't do this very often, and it takes a lot of memory (potentially) because you are holding things in memory until you get back to a beginning of some kind (and stop calling the function), but this is a great (and the only) solution for tracking back through dichotomous decision-making algorithms such as taxonomic keys. The example on 153-155 illustrates.