Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Different ways to include a php file into another PHP file

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 613
    Comment on it

    PHP provides different functions to include content of one php file into another file before execution of the file. There are following four methods which can be used to include files :

     

    1. include

    2. include_once

    3. require

    4. require_once

     

    The above all statements are same in usage all are used to copy the content of one file into another file, but they have some technical difference. This article provides the description of all these statements and also describe the difference between them.

     

        Generally, files are included on the basis of the specified file path. If the file is not found in the specified path, the file is checked under directory by calling script and the current working directory.

    For testing these functions we use two files : title.php and content.php.

     

    title.php

    <h2>Test</h2>

     

    content.php

    <?php
    echo "<p>This is testing of PHP functions.</p>";
    ?>

     

    1. include

    The include statement load the content of a file (included by specifying the path) into another file which calls the include. The function copies all the text from a specified file into the calling file before the file executed by the server.

    On failure the include statement generates a warning (E_WARNING) and the execution of the script will continue.

     

    Syntax : include 'filename'  or  include('filename')

    filename : Specifies the file to include. The complete path of file.

     

    Result : On successful loading of file include returns 1 and on failure, it returns FALSE and also generates a warning.

     

    Example :

    <?php
    //----General example----
    include "title.php";
    include "content.php";
    echo "======================<br>";
    
    //-----check for return type-----
    echo include("title.php");
    echo "<br>========================<br>";
    
    //---wrong path----
    echo include("../title.php");
    ?>

    Output :

    Test
    This is testing of PHP functions.
    ======================
    Test
    1
    ========================

    Warning: include(../title.php): failed to open stream: No such file or directory in /var/www/html/test.php on line 16

    Warning: include(): Failed opening '../title.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/test.php on line 16

     

     

    2. include_once

    include_once is also exactly same as include statement, it also include and evaluate the specified file. But the only difference is that it includes specified file only once, if the file already included then in next time file not included again.

     

        include_once can be helpful when the same file is included more than once on the same script, so it includes file once during execution of a script which avoids the problem of redefining functions and reassigning values to variables on each call.

     

    Syntax : include_once 'filename'  or  include_once('filename')

    filename : Specifies the file to include. The complete path of file.

     

    Result : The result of include_once is same as include. If the content of the file was already included, include_once return TRUE.

     

    Example :

    <?php
    include_once "title.php";
    include_once "content.php";
    echo "======================<br>";
    include_once "title.php";
    include_once("content.php");
    ?>

    Output :

    Test
    This is testing of PHP functions.
    ======================

     

    It is clear from the above example, we include both files two time but content of files include once only.

     

     

    3. require

    require is similar to include statement, it also loads the content of the specified file into another file. The main difference between them is that, On failure require generates a fatal (E_COMPILE_ERROR) error and halt the execution of the script. While include generates a warning and keep continue execution of the script.

     

        require statement can be used where we need the content of the specified file, so if file not included the execution of the script also halt.

     

    Syntax : require 'filename'  or  require('filename')

    filename : Specifies the file to include. The complete path of file.

     

    Result : On success require returns 1 and on failure, it generates a fatal error and stops the execution of file.

     

    Example :

    <?php
    //----General example----
    require "title.php";
    require "content.php";
    echo "======================<br>";
    
    //---wrong path----
    echo require("../title.php");
    ?>

    Output :

    Test
    This is testing of PHP functions.
    ======================

    Warning: require(../title.php): failed to open stream: No such file or directory in /var/www/html/test1.php on line 12

    Fatal error: require(): Failed opening required '../title.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/test1.php on line 12

     

     

    4. require_once

    Just like include_once(), require_once() also include the specified file only once. It also returns the fatal error on failure.

     

    Syntax : require_once 'filename'  or  require_once('filename')

    filename : Specifies the file to include. The complete path of file.

     

    Example :

    <?php
    require_once "title.php";
    require_once "content.php";
    echo "======================<br>";
    require_once("title.php");
    require_once("content.php");
    ?>

    Output :

    Test
    This is testing of PHP functions.
    ======================

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: