목차
php中的include和require的区别
Welcome to my home page!
php教程 php手册 php中的include和require的区别

php中的include和require的区别

Jun 13, 2016 am 09:28 AM

php中的include和require的区别

主要关注红色标记语句即可。

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

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


PHP include and require Statements

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

The include and require statements are identical, except upon failure:

    require will produce a fatal error (E_COMPILE_ERROR) and stop the scriptinclude will only produce a warning (E_WARNING) and the script will continue

    So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.

    Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be 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 looks like this:

    echo "

    Copyright © 1999-" . date("Y") . " W3Schools.com

    ";
    ?>

    To include the footer file in a page, 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 use this menu file. Here is how it can be done (we are using a

    element so that the menu easily can be 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 be 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 is included with the include statement and PHP cannot find it, the script will 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 will not 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 file is not found.
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 채팅 명령 및 사용 방법
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)