|
 |
|
Oracle Tips by Burleson
|
Includes
Once the functions are defined, it is useful to
have them separated into their own file, which can be included from
many programs, as they are needed. C and C++ have the #include
directive for that purpose, Perl has require and use. PHP, of
course, has its own directives for this purpose, namely require
and include.
Require and include are almost
identical, except in the way they handle errors. While include
produces a warning, require produces a fatal error. When
including a file, the web server executing the PHP script drops into
the HTML mode. This means that any PHP code within the included file
must be enclosed between the delimiter tags <?php and ?>.
Better options are require_once and include_once
directives. The once versions keep track of what has been
included. If the file has already been included, it is not included
again. What would example9.php (the one with factorial) look
like if the factorial function is separated in its own file, factorial.php?
It would look like the following:
$ cat
example9b.php
#!/usr/local/bin/php
<?php
require 'factorial.php';
$a=5;
echo "Factorial($a)=".factorial($a)."\n";
?>
The factorial.php file looks as expected:
$ cat
factorial.php
<?php
function factorial($n) {
if ($n>0) return($n*factorial($n-1));
else return(1);
}
?>
And, of course, the execution is just as easy:
$
./example9b.php
Factorial(5)=120
PHP supports nested includes. This means that the
included file can itself include another file. All files are included
in the syntactical scope of the include statement itself. If
the included file includes variable definitions, those variables are
local to the function which included the file.
The last question is, how does PHP know where to
find the files to include? There is a parameter called include_path
in the php.ini parameter file which serves precisely for that
purpose. This parameter is mentioned several more times in upcoming
chapters.
See
code depot for complete scripts
This is an excerpt from the book
Easy Oracle PHP. You can get it
for more than 30% by buying it directly from the publisher and get
instant HTML-DB scripts from the code depot:
 |
Easy Oracle PHP
Create Dynamic Web Pages with Oracle Data
Includes online HTML-DB code depot
Buy it now for 30% off
- Only $19.95
|
HTML-DB support:
 |
For HTML-DB development support just call to gat an
Oracle Certified professional for all HTML-DB development
projects. |
|