0% found this document useful (0 votes)
21 views

Web App Prog CH5

Uploaded by

Mr. Shuaimi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Web App Prog CH5

Uploaded by

Mr. Shuaimi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

[Please insert any

relevant photo
around this box] CHAPTER

5
“Talk is cheap. Show me the
code.”
― Linus Torvalds

Reusing PHP Code


Subtopics:-
5.1 Include a Code File
5.2 Include File Automatically
5.3 Store include Files Securely
5.4 Define and Used a Function
5.5 Using variables in a Function
5.6 Pass Values to a Function

5.1 Include a Code File

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

5.2 Include File Automatically

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.

5.3 Store include File Secure

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.

If an include file is accessed directly in the browser, instead of accessed by an include


statement in a PHP script and does not have a .php extension, the code is treated as HTML
code and is usually displayed on the web page. This could be a security risk. On the other
hand, if the file is named with a .php extension, it is processed by PHP when accessed
directly executing the PHP statement in the include file outside their expected context. This
could also be a security risk. The best way to protect the content of your include files is store
them in a directory outside your web space. Web site visitor cannot access the files directly
when they are stored outside the web space. If you specify the directory in the
include_path, you do not need to specify the path in your PHP code.

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.

5.5 Using variable in a Function

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.

5.6 Pass Value in a 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:

function functionname($varname1, $varname2, …)


The value are stored in the variable names in the order on which they are received. If the
function call does not send as many values as the functions expects the missing variable
do not exist. If the warning level is turned on a warning message informs you that an
argument is missing, but the script continues to execute. If you send too many values, the
function ignores the extra values.

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

You might also like