Home > DeveloperSection > Blogs > Include and Require function in PHP

Include and Require function in PHP


PHP PHP 
Ratings:
0 Comment(s)
 3022  View(s)
Rate this:

Include and Require function in PHP

Before discussing about include () and require () function in PHP first we will described what is Server Side Includes (SSI)? Server Side Includes (SSI) is an efficient way through which we can insert all content of one PHP file to another PHP file before the server executes it, with include() or require() function.

The two functions are identical in every way, except how they handle errors:

·         include() generates a warning, but the script will continue execution

·         require() generates a fatal error, and the script will stop

These two functions are used to create functions, headers, footers, or elements that will be reused on multiple pages.

include () function:

 include () function takes all the content in a specified file and includes it in the current file. If an error occurs, include () function generates a warning, but the script will continue execution.

Example:

<?php

   //Include all content of Master page

   include ("MasterPage.php");

?>

require () function :

require () function works same as include function except when an error occurs in the script then script will be stoped immediately.

You can differentiate these two by following example:

<?php

  // Include wrong file(i.e does not exist) with include function

    include 'Temporary.php'.'</br>';

// display a message after including 'Temporary.php' file

   echo '</br>'.'Message: Temporary file included';

?>

<?php

    // Include wrong file(i.e does not exist) with require function

   require 'Temporary.php'.'</br>';

   // display a message after including 'Temporary.php' file

echo '</br>'.'Message: Temporary file included';

?>


Don't want to miss updates? Please click the below button!

Follow MindStick