Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between include(), require() and require_once()

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 133
    Comment on it

    The include() function

    The include() function is used to include the whole text of a specific file to another file where we want to include that file. While including the file if there will be any error in loading the file, it will only produce the WARNING and script will be continued.

    e.g of include() function is:
     

    
    
    <html>
    
    <head>
    
    <title>Include file</title>
    
    </head>
    
    <body>
    
    <?php include ("nav.php"); ?>
    
    <h2>Welcome</h2>
    
    </body>
    
    </html>
    
    
    

    File which is included above (nav.php):

    <a href="/">Home</a>|
     <a href="about.php">About Us</a> |
     <a href="faq.php">FAQ |
     <a href="contact.php">Contact Us</a>

    The output will be the combination of both the files.

     

    The require() function

    The require() function is just like an include() function but the only difference is if file is not found the require() will show COMPILE error and stop the script execution.

    e.g of require() function is:

    if we include a wrong file, then it will stop execution and nothing will be displayed.

    <html>
     <body>
     <?php require("nonexisting.php"); ?>
     <p>e.g of including wrong file</p>
     </body>
     </html>

    In the above e.g, the include function will give the following o/p:-

    e.g of including wrong file

     

    THE require_once() Function

    This function is used most when we construct the page. The require_once() function is used to display header and footer which is used only once in the page. The reqired_once() check the included file during the execution of the script, if that code is already being included then it will not include it again. It produces a fatal error and stop the execution.

    e.g of require_once() function is:

    require_once 'header.php';
     <div id="content">
    
    
     </div> 
    require_once 'footer.php';

     

 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: