Develop PHP Website for Learning Web Design Free Online
  Home   |   Forums   |   DevCMS
  Join   |   Log In
 
   

PHP Tutorials - Learn PHP

Tutor at Develop PHP
Including and Requiring Files
By: Adam      Created: Jul 15, 2009      Views: 2552 Tweet This Page  Post page to Facebook  Post page to Facebook

When you use the include(), include_once(), require(), or require_once() statements in PHP you are instructing a certain file and all of its contents to be "included" into your script that is calling it. This is very handy for making modular reusable code structures, even in simple applications.

The include() statement will include the desired file if the file can be found through the path specified in the statement. If the desired file cannot be found, no errors will occur, but the file and its contents will not be included. The same file can be included multiple times into the calling script. Using include_once() you can command the calling script to only include a certain file one time and no more.

The require() statement works a lot like the include() statement in the respect that will bring another file and its contents into the calling script. The big difference is that using require() will "require" the file to be at the specified location on server. If the desired file to require cannot be found, an error will occur in your script, and the script will terminate. Using require_once() you can command the calling script to only require a certain file one time and no more.

For the following code examples we first need a file to include, let's create that now:

Name this file "my_file.php" and make sure it is living in the same directory as the calling file:
Learn HTML
<?php 
$var1 
"Hello World!";     
?>

Now let's include that file into our main calling file by doing the following:
Learn HTML
<?php 
include ("my_file.php");

echo 
$var1;
?>

Develop PHP browser display window
Hello World!




Here is an example of using each type:

Learn HTML
<?php 
include ("file1.php");
include_once (
"file2.php");
require (
"file3.php");
require_once (
"file4.php");

// All 4 files would be included into your main script
// file1.php and file3.php can be included multiple times
// file2.php and file4.php cannot be included multiple times
?>


Here are alternate ways of writing your include and require statement values that all work the same:

Learn HTML
<?php 
// All of the lollowing syntax will work the same
include ("my_file.php");
include 
"my_file.php";
include 
'my_file.php';
?>


                                   Communicate Between Two Servers Using PHP Includes


Comment on Including and Requiring Files




Search Tags:
Including   ·  and   ·  Requiring   ·  Files   ·  
 
 
Arbitrary Links and Archives
Home
Active Forums
Members
SIte News
Link To Us
Gear
2009 Forum Archive
Programming Courses
Learn HTML
Learn CSS
Learn PHP
Learn MySQL
Learn Javascript
Learn jQuery
Learn ActionScript 3.0
Learn Java
Learn XML
DevelopPHP Requires Flash Player
Get Adobe Flash player
___________________________________________

Terms of Use  •  Privacy  •  Admin Notes

©2010 developphp.com   |   Powered By FlashBuildingHolder