PHP Include and Required

In php there are functions that allow you to include files into others.  These functions can help you to split large amounts of code and content.  PHP “Include” behaves similar to “Require” with only one exception “Error handling”.

  • Include: If this function fails it will throw a warning and continue with the execution.
  • Include_once : Works similar to include() except this will verify if the code has not been  included before. This function ensures that the file is called once.
  • Require: If this function fails, it will give you a fatal error and terminate the execution.
  • Require_once: Works similar to require() except this will verify if the code has not been included before.

When you include a file, all variables, functions or classes become available on the file that is doing the call.

Php include() and require()

File name : cars.php

<?
 $car_color = "White";
 $car_make  = "Some Make";
 $car_year  = "0000";
 echo "<h5>Car info</h5> ";
 ?>

File name: cars_info.php

<?
 // uncoment and comment the code to see how its works
 include("cars.php");
 //require("cars.php");

 echo "The car color is: ".$car_color."\n";
 echo "The car make is: ".$car_make."\n";
 echo "The car was build on : ".$car_year."\n";
 ?>

If you run this script you should get a result like this :

The car color is: White
The car make is: Some Make
The car was build on : 0000

Using  _once():

This time we are going to call the file twice using include or require:

<?
include("cars.php");
//require("cars.php");
echo "The car color is: ".$car_color."</br>";
echo "The car make is: ".$car_make."</br>";
echo "The car was build on : ".$car_year."</br>";

include("cars.php");
// require("cars.php");
// As you can see we are calling the file cars.php twice.
 ?>

Result :

Car info

The car color is: White
The car make is: Some Make
The car was build on : 0000

Car info

Note: car info is been display twice.

Using include_once or require_once

<?
include_once("cars.php");
// require_once ("cars.php");
echo "The car color is: ".$car_color."</br>";
echo "The car make is: ".$car_make."</br>";
echo "The car was build on : ".$car_year."</br>";

include_once("cars.php");
// require_once ("cars.php");
// We are calling the file cars.php twice again.
?>

Result:

Car info

The car color is: White
The car make is: Some Make
The car was build on : 0000

Note: The resul is different . You should see Car info  once.

Error Handling:

The file cars_file.php doesn’t exist.

Using include:

Warning: include(cars_file.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\your file\your file\car_info.php on line 2

Warning: include() [function.include]: Failed opening ‘cars_file.php’ for inclusion (include_path=’.;C:\xampp\php\PEAR’) in C:\xampp\your file\your file\car_info.php on line 2

Notice: Undefined variable: car_color in C:\xampp\your file\your file\car_info.php on line 5
The car color is:

Notice: Undefined variable: car_make in C:\xampp\your file\your file\car_info.php on line 6
The car make is:

Notice: Undefined variable: car_year in C:\xampp\your file\your file\car_info.php on line 7
The car was build on :

Using require:

Warning: require(cars_file.php) [function.require]: failed to open stream: No such file or directory in C:\xampp\your file\your file\car_info.php on line 2

Fatal error: require() [function.require]: Failed opening required ‘cars_file.php’ (include_path=’.;C:\xampp\php\PEAR’) in C:\xampp\your file\your file\car_info.php on line 2

 

Advantage of using Include or Required

  • Code can be reusable.
  • Standardize your code
  • The code only needs to be changed in one place instead of changing all the scripts.

Eg: If you are using a file like connection.php that holds your mysql connection, you just need to change it once.

Common use  :

  • Secure.php
  • Database_Connection.php
  • Header.php
  • Menu.php
  • Content.php
  • Footer.php
This entry was posted in PHP and tagged , . Bookmark the permalink.

2 Responses to PHP Include and Required

  1. Pseudo says:

    You have it wrong in your intro.
    Require throws a E_COMPILE_ERROR and will stop the execution, include just emits a warning.

  2. robert says:

    Thanks , It was a mistake when writing the post.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>