The difference between include and require in PHP

Source: Internet
Author: User
Tags execution header include variables php file php code php tutorial vars

The main focus is on red tagged statements.

The include (or require) statement takes all Text/code/markup this exists in the specified file and copies it into the File that uses the include statement.

Including the files are very useful when you are want to include the same PHP, HTML, or text on multiple pages of a website.

PHP include and require statements

It is possible to inserts the content of one PHP file into another PHP file (before the server executes it), with the INCLU De or require statement.

the include and require statements are identical except upon failure:

    Require would produce a fatal error (E_COMPILE_ERROR) and stop the scriptinclude'll only produce a warning (e_warning) a d The script would continue

    So, if you are want the execution to go in and show users the output, even if the include file is missing, use the include STA Tement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always with the require statement to include a K EY file to the flow of execution. This would help avoid compromising your application "s security and integrity, just in-case one key file is accidentally mis Sing.

    Including files saves a lot of work. This means can create a standard header, footer, or menu file for all your Web pages. Then, when the header is needs to is updated, you can only update the header include file.

    Syntax

    Include " filename";

    Or

    Require " filename";
    PHP include examples

    Example 1

    Assume we have a standard footer file called "footer.php", that's looks like this:

    -->echo "

    Copyright©1999-". Date ("Y"). "W3schools.com

    ";
    ?>

    To include the footer file in a page and use the Include statement:

    Example


    Welcome to my home page!


    Some text.


    Some more text.


    -->



    Run example?

    Example 2

    Assume we have a standard menu file called "menu.php":

    -->echo "Home-
    HTML Tutorial-
    CSS Tutorial-
    JavaScript Tutorial-
    PHP Tutorial ";
    ?>

    All pages in the Web site should with this menu file. Here are how it can do (we are using a element so this menu easily can styled with CSS later):

    Example






    Welcome to my home page!


    Some text.


    Some more text.





    Run example?

    Example 3

    Assume we have a file called "vars.php" with some variables defined:

    -->$color = "Red";
    $car = "BMW";
    ?>

    Then, if we include the "vars.php" file, the variables can is used in the calling file:

    Example


    Welcome to my home page!


    -->echo "I have a $color $car.";
    ?>



    Run example?
    PHP include vs. require

    The Require statement is also used to include a file into the PHP code.

    However, there is one big difference between include and require; When a file was included with the include statement and PHP cannot find it, the script would continue to execute:

    Example


    Welcome to my home page!


    -->echo "I have a $color $car.";
    ?>



    Run example?

    If We do the same example using the require statement, the Echo statement won't be executed because the script Execution dies after the Require statement returned a fatal error:

    Example


    Welcome to my home page!


    -->echo "I have a $color $car.";
    ?>



    Run example?
    Use require when the ' file is ' required by the application.

    Use include when the ' file is not ' required and application should continue when ' not ' found.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.