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

Include and Require function in PHP


PHP PHP 
Ratings:
0 Comment(s)
 4805  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:

Now let’s assume that we have a master page, called ‘MasterPage.php’. To include this file into ‘home.php’ page, use include () function.

Code of ‘Home.php’ page:

  <!DOCTYPEhtml>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

        <title>PhpInclude File</title>

    </head>

    <body>

        <table id="bodyText">

            <tr>

               <td>

                 <div id ="leftMenu">

                   <?php

                           //Include all content of Master page

                      include ("MasterPage.php");

                    ?>

               </div>

              </td>

          <td>

           <p>Welcome to Home Page</p>

           </td>

       </tr>

    </table>

       

    </body>

</html>

 

Here you can use require () function in place of include () function because both are identical in every way, except how they handle error.

All pages in the Web site should include this master file. Here is how it can be done:

Code of ‘MasterPage.php’ page:

<a href="Timestamp.php" alt="Timestamp" > Timestamp</a></br>

 <a href="Welcome.php" alt="Welcome"> Welcome</a></br>

  <a href="Helloword.php" alt="Hello"> Hello word</a></br>

 

Now execute the ‘Home.php’ page, following output look like this.

Include and Require function in PHP

If you look at the source code of the page above (in a browser), it will look like this:

Include and Require function in PHP

require () function :

require () function works same as include function except how they handle errors.

Let’s we have an example, how they handles error.

Example: Include php file with the help of include() function.

 <!DOCTYPEhtml>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

        <title></title>

    </head>

    <body>

        <?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';

        ?>

    </body>

</html>

 

Output:

Include and Require function in PHP

Example: Include php file with the help of require () function.

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

        <title></title>

    </head>

    <body>

        <?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';

        ?>

    </body>

</html>

Output:

Include and Require function in PHP

From above two outputs it is clear that If an error occurs (when including php file), include () function generates a warning, but the script will continue execution and message is going to be display but require () generates a fatal error, and the script will stop.


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

Follow MindStick