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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 203
    Comment on it

    In order to include the content of another PHP page in the current PHP page we can use two methods provided by PHP which are:

    1. include()
    2. require()

    With the above two methods we can make HEADER, FOOTER, FUNCTIONS when user is working on another page and then they can able to include all those methods which the user have included in the previous page.  This will help a lot in creating website with minimal effort and if there changes need to be made we can do so easily by just changing the code of that include file.

    The only difference between include() and require() is that -

    include() will display all the data of the included file into the file that called include() function, but if the file does  not exist it will show a warning but will continue executing the script.

    On the other hand require() will display all the data of the included file into the file that called require() function, but if the file does not exist it will show a error and will halt the execution of the script.

    For example : if we try to include a header.php file which does not exist then the output of the two methods will be:

    • When using include()
    <html>
       <body>
       
          <?php include("header.php"); ?>
          <p>Text will be displayed even if file does not exist</p>
          
       </body>
    </html>

    The output will be:

    Text will be displayed even if file does not exist
    • When using include()
    <html>
       <body>
       
          <?php require("header.php"); ?>
          <p>No output will be displayed if the file does not exist</p>
          
       </body>
    </html>

    No OUTPUT in this case.

 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: