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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 103
    Comment on it

    We can include the content of a PHP file into another PHP file before executed by the server.

    There are two type of function to include one php to another.

    The include() function.

    The require() function.

    The include() Function

    It take all the text to a specific file and copies it to the file that uses the include function. If a problem occur in loading a file then the include() function generates a warning but the script will continue for the execution.

    create a file menu.php with the following content

    <a href="http://www.findnerd.com/index.htm">Home</a> - 
    <a href="http://www.findnerd.com/ebxml">ebXML</a> - 
    <a href="http://www.findnerd.com/ajax">AJAX</a> - 
    <a href="http://www.findnerd.com/perl">PERL</a> <br />

    create test.php file can have following content

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

    The result

    Home

    ebXML

    AJAX

    PERL

    This is an example to show how to include PHP file!

     

    The require() Function

    The require() function take all the text to a specified file and copies it into that file that uses the include function. If a problem occur in loading a file then the require() function generates a fatal error and stop the execution.

    there is basically no difference between require() and include() except they both handle error conditions. It is told to use the require() function instead of include() because scripts should not continue exectuion if the files are missing or misnamed.

    ex

    <html>
       <body>
           
           <?php require("xxmenu.php"); ?>
           <p>This is an example to show how to include wrong PHP file!</p>
       
       </body>
    </html>

    The file execution will now stop and show nothing.

 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: