What is include and require function in php ?

you can Add the content of one php page into another php page before the server executes it.

  • in php, The include and require statements are used to insert the one page to another page.
  • Include commands provides the opportunities to include several php pages within a single page.
  • SSI stands for Server Side Include and means that you can include a file as part of your document while still on the server before it is executed and before the data reaches the client. One reason for doing this is that often data such as a header, footer, or variables are repeated in many places on the site. Using SSI allows these to each be stored in only one place, and thus only updated in one place when changes are made.


    There are two ways to include file in PHP.
  • include
  • require
  • Advantage :-

    Code Reusability: By the help of include and require, we can reuse HTML code or PHP script in many PHP scripts.

    Syntax:
    include 'file.php';
    include('file1.php');
    /******** Or *********** /
    require 'file.php';
    require('file1.php');

    Example :

    menu.php

    <a href="http://itechtuto.com">Home</a> |
    <a href="http://itechtuto.com/aboutus">aboutus</a> |
    <a href="http://itechtuto.com/contactus">contactus</a>


    Output :-

    Home | aboutus | contactus

    Example : index.php

    index.php

    <html>
    <head>
    <title> </title>
    </head>
    <body>
    <?php include "menu.php";?>
    /****************** OR ************* /
    <?php require "menu.php";?>
    <h3>Here include the menu.php page.</h3>

    </body>
    </html>


    Output :-

    Home | aboutus | contactus
    Here include the menu.php page.

    Differences between include and require. :-

    NOTE :- include and require are identical, except upon faulure.
    1. include will only produce a warning. (E_WARNING) and the script will continue.
    2. require will produce a fatal error. (E_COMPILE_ERROR) and stop the script.
    3. when you use include file and file doesn't exist then your program show warning. and your program continue execute.
    4. when you use require and file doesn't exist then your program show fatal error. and your program terminate.

    What is difference between include and include_once.

    The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.


    require()

    require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, don?t hesitate to use require() if you want a missing file to halt processing of the page.


    what is difference betwenn include_once and require_once.

    The require_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being that if the code from a file has already been included, it will not be included again.





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here