Page 75 - Open Soource Technologies 304.indd
P. 75
Unit 3: Flow-Control Statements and PHP in Web Page
If the allow_url_fopen option is enabled through PHP’s configuration file, php.ini, you can include Notes
files from a remote site by providing a URL instead of a simple local path like:
include ‘http://www.example.com/codelib.inc’;
If the filename begins with “http://” or “ftp: //”, the file is retrieved from a remote site and
then loaded.
Files included with include and require can be arbitrarily named. Common extensions are .php,
.inc, and .html. Note that remotely fetching a file that ends in .php from a web server that has PHP
enabled fetches the output of that PHP script. For this reason, we recommend you use .inc for
library files that primarily contain code and .html for library files that primarily contain HTML.
If a program uses include or require to include the same file twice, the file is loaded and the code
is run or the HTML is printed twice. This can result in errors about the redefinition of functions or
multiple copies of headers or HTML being sent. To prevent these errors from occurring, use the
include_once and require_once constructs. They behave the same as include and require the first
time a file is loaded, but quietly ignore subsequent attempts to load the same file. For example,
many page elements, each stored in separate files, need to know the current user’s preferences. The
element libraries should load the user preferences library with require_once. The page designer
can then include a page element without worrying about whether the user preference code has
already been loaded.
Code in an included file is imported at the scope that is in effect where the include statement
is found, so the included code can see and alter your code’s variables. This can be useful; for
instance, a user-tracking library might store the current user’s name in the global $user variable:
// main page include ‘userprefs.inc’; echo “Hello, $user.”
The ability of libraries to see and change your variables can also be a problem. You have to know
every global variable used by a library to ensure that you do not accidentally try to use one of
them for your own purposes, thereby overwriting the library’s value and disrupting how it works.
If the include or require construct is in a function, the variables in the included file become
function-scope variables for that function.
Because include and require are keywords, not real statements, you must always enclose them
in curly braces in conditional and loop statements:
for ($i=0; $i < 10; $i++) { include “repeated_element.html”; }
Use the get_included_files( ) function to learn which files your script has included or required. It
returns an array containing the full system path filenames of each included or required file. Files
that did not parse are not included in this array.
3.6 Embedding PHP in Web Pages
Although it is possible to write and run stand alone PHP programs, most PHP code is embedded
in HTML or XML files. This is, after all, why it was created in the first place. Processing such
documents involves replacing each chunk of PHP source code with the output it produces when
executed.
Because a single file contains PHP and non-PHP source code, we need a way to identify the regions
of PHP code to be executed. PHP provides four different ways to do this.
As you will see, the first, and preferred, method looks like XML. The second method looks like
SGML. The third method is based on ASP tags. The fourth method uses the standard HTML
<script> tag; this makes it easy to edit pages with enabled PHP using a regular HTML editor.
LOVELY PROFESSIONAL UNIVERSITY 69