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

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

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




Recommended for You

Tagged Articles

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

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • More O'Reilly Sites
  • igniteshow.com
  • makerfaire.com
  • makezine.com
  • craftzine.com
  • labs.oreilly.com
  • Partner Sites
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com