PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

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 ?>.

Programming PHP

Related Reading

Programming PHP
By Rasmus Lerdorf, Kevin Tatroe


Read Online--Safari
Search this book on Safari:
 

Code Fragments only

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




Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com