PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


PHP Foundations Multiple File PHP Scripts

by John Coggeshall
06/20/2002

Although it's not ever truly necessary, many times it becomes very important to have the ability to separate PHP code into multiple files to ease organization and promote the idea of reusing common functions within your PHP scripts.

Thankfully, PHP supports four different language constructs and functions to allow for the import of code from other files, which I will discuss now.

Including Code in Your PHP Script

There are many ways to include code in PHP. Depending on your system's configuration, you can include files from both your local file system or even from remote servers that are configured to do so. There are many different tricks and useful ways to use this behavior in PHP and I'll try to cover most of them here.

The most basic of all code imports in PHP can be done with the use of PHP's include() statement. The syntax for the include statement is as follows:

include <path or URL to the file to include>

When an include statement is encountered by PHP, the PHP engine immediately stops parsing PHP code and attempts to import the code directly over the calling include statement. This means that whatever code is contained within the requested file will replace the include statement and will have available to it any variables that were defined, as well as be constrained to the same variable scope. Furthermore, any PHP code that is included must also be wrapped in standard PHP code tags such as <?php and ?>.

Here's an example. Below are two different files. The first file, myscript.php is the file that has been executed by PHP initially. The second file, included.php is another PHP script that is meant to be included:

myscript.php

<?php
for($i = 0; $i < 10; $i ++) {
include 'included.php';
}
// the isset() function determines if the
// passed variable actually exists or not
if(isset($testvar)) echo '$testvar is set<br />';
?>

included.php

<b><u>
<?php echo 'The value of the variable is: $i<br />'; ?>
</u></b>
>?php $testvar = true; ?<

In the above example (assuming, of course, the file included.php is in the same directory as myscript.php) the output will be:

The value of the variable is: 0
The value of the variable is: 1

...

The value of the variable is: 9
$testvar is set

Notice that the $i variable was not defined anywhere in the included.php script where it is used; instead it has been automatically inherited from the calling script.

Note: The above output is not in error. Because the above script uses single quotes instead of double quotes to check for the existence of $testvar in the final echo statement, it will be displayed to the client as is, and the output will reflect the string '$testvar' instead of the value contained within the PHP variable of the same name.

Pages: 1, 2

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: