Web App Prog CH5
Web App Prog CH5
relevant photo
around this box] CHAPTER
5
“Talk is cheap. Show me the
code.”
― Linus Torvalds
Your Web site scripts may need to perform a task repeatedly such as displaying the
company address or storing data in a database. Efficient development dictates that you
write PHP code to perform a certain task and use the same code whenever the task is
required. One method for reusing code is PHP include files. You can store as many lines of
code as you need in a file, separate from your script and insert (called include) the file into
the script wherever you need it.
PHP provides four statements for inserting a file into script: include, include_once, require,
and require_once. You can use the following format for all four, passing a string or a
variable:
include(path/filename);
41 | P a g e C h a p t e r 5 : R e u s i n g P H P C o d e
The include/require statement behave identically except for their behavior when they
cannot open the specified file. The include statement issues a warning, the require
statement issues a fatal error. The include_once/require_once statement behave
identically to the include/require statement, except that a file is included only once. If you
use include_once to include a file, PHP will not include the same file again; even if you use
another include statement.
The code in the include file is always treated as HTML code, even if the include statement
is located in a PHP section of the script. Consequently if the include file contain PHP code,
you must add PHP tags inside the include file. You can name include file with the name
and store them in any directory. However, where you store include files and what you name
them can be a security issues.
• To reuse existing code
o Store lines of code in a file, save separately and insert (include) the file into the
concern script whenever its needed
• Four statements for inserting a file into a script:
o include,
o include_once include require
o require,
Issue a warning if fails Issue a fatal error if fails
o require_once
You can tell PHP to process a file at the start of every script and/or at the end of every script.
The files are treated as if they were included with an include statement. If you have a set of
functions or classes that you want to be available to every script, you can store them in an
include file that you autoprepend to all your script. Setting in php.ini specifies which file to
42 | P a g e C h a p t e r 5 : R e u s i n g P H P C o d e
process at the beginning or at the end. Open php.ini in a text editor and look for the
following settings:
Auto_prepend_file =
Auto_append_file =
Add the name of the file following the equal sign. You can add either an auto_prepend file
or an auto_append file or both. Because the specified files are processed as if they were
included with an include statement, the include file features all apply to the auto_prepend
and auto_append files. For example you must include the PHP opening and closing tags in
the include file if it contains PHP statement, not just HTML code. The auto_prepend and
auto_append files must be located in the include path.
You can disable auto_prepend or auto_append files by adding “none” after the equal sign.
If the script ends with an exit() statement, instead of continuing to the closing PHP tag, the
auto_append file is not precessed.
PHP looks for include file in directories that you specify with the include_path setting. A
specified directory where you store include files can be located anywhere on your
computer hard disk. You do not need to store include files in your Web space. You can store
them in a directory that cannot be accessed from the Web. You can name your include
files with any valid filename. Using a filename extension, such as .inc is useful for organization
purposes enabling you to see immediately whether a file is a PHP script or an include file.
43 | P a g e C h a p t e r 5 : R e u s i n g P H P C o d e
5.4 Define and Used a Function
When you need to perform the same task at different point in a program or in different
program you can create a function, a block of statement that perform a specific task and
use it wherever you need to perform the task. You can create a function as follows:
function
functionname(varname1, varname2, …)
{
block of statement
return;
}
You can pass values to the function, which it can use in the code block by listing variable
names to contain the values. You can return values from the function to the main script with
the return statement, which also need the function. To use a function, you include it name
in a statement, referred to as calling the function. A function may be defined anywhere in
the script, but the usual practice is to put all the functions together at the beginning or end
of the script. Functions that you plan to use in more than one script can be in a separate
file that you include in scripts that need the functions. PHP provides many built-in functions,
which are the same as the functions you create, except that PHP does the work for you.
You can create and use variable inside your functions. Where you can use the variable,
called the scope, depend on how you define the variable. When you create the variable
with an assignment statement inside a function, the variable is local t the function. You can
use the variable in the function, but it is not available in the main script, outside the function.
If you assign the value to a variable inside a function and then echo the variable in the
main script, after the function call the variable does not exist. You can make a variable
global, instead of local using a global statement as follow:
global $varname;
a global variable can be used anywhere in the script. If you create a variable in a function
and define it as global the variable still exists after the function finishes executing. Similarly if
a variable is created outside the function, you cannot use it inside the function unless it is
global. If you create a variable in the script with an assignment statement before the
function call and then assign a value to the variable inside the function, the variable you
create in the function are not the same variable. They are two different variables with the
44 | P a g e C h a p t e r 5 : R e u s i n g P H P C o d e
same name. you must use the global statement before the value assigned to the variable
inside the function is also assigned to the variable outside the function.
You can pass values to a function by putting the value between the parentheses when you
call the functions, as follows:
functionname(value1, value2, …)
The values can be passed directly or in variable. The values can be any data type. The
function must be defined to expert the values as follows:
If the wrong data type is passed, the function will not work as expected. You can use a
conditional statement inside a function to execute the statement if the wrong data type is
passed. You can test the data type using function, such as is_int or is_string. You can
define a default value for an argument by assigning a default value for an argument by
assigning a default value in the function statement as follows:
function
functionname($varname1, $varname2, …)
45 | P a g e C h a p t e r 5 : R e u s i n g P H P C o d e
The function uses the defined default value when a value is not passed. If a value is passed,
the function uses the value instead of the default.
46 | P a g e C h a p t e r 5 : R e u s i n g P H P C o d e