Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Including files in PHP

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 109
    Comment on it

    In PHP there is a feature which allows to write code in a different file and call it where ever there is a need for executing the code. this can be done by using the include and require functions.

    with the help of the these two functions we can perform the same :

    • The include() Function
    • The require() Function

    these two function allows to call the file to execute. But the main difference between both is that

    The Include function does the loading of the code of the file and if there is any error in loading then the include will generate the error message and displays it but continue execution.
    On the other hand the require function loads the file but if any error occur then the require function will displays the fatal error and halts the execution of the code.

    The example of the include function as :

    <html>
       <body>
    
          <?php include("menu.php"); ?>
          <p>This is an example to include PHP file!</p>
    
       </body>
    </html>
    

    In the above code if there is any error even though the execution continues and displays the error message.
    while in require it is not the case.

    <html>
       <body>
    
           <?php require("menu.php"); ?>
           <p>This is an example wrong PHP file!</p>
    
       </body>
    </html>
    

    In the above code if the required function is used to call the file and any error occurred then it halts the execution and doesn't proceed.

    It is always recommended to use require() instead of include() function, as the execution should halt when any error occurred.

 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: