본문 바로가기

Languages/others

PHP-include 와 require 의 차이점

 

출처:

require: http://php.net/manual/en/function.require.php
include: http://php.net/manual/en/function.include.php

 

 

  • require
    • 원문: require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.
    • require 는 에러가 발생될 때 E_COMPILE_ERROR 수준으로 오류가 생성됩니다. 즉, 스크립트에러가 발생하게 되면 계속 컴파일을 하는 include 와 달리 오류를 발생하며 더 이상 페이지를 로드하지 않게 됩니다.
    • C#에서 #include 와 같습니다.
  • include
    • 원문:
    • The include statement includes and evaluates the specified file.

      The documentation below also applies to require.

      Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.

      If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — theinclude_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

      For more information on how PHP handles including files and the include path, see the documentation for include_path.

      When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

    • include 는 외부 파일을 불러올 때 사용합니다. 페이지에서 로드할 때마다 새롭게 갱신(로드)하여 페이지에서 컴파일되어 보여지게 되는데, 스크립트의 에러가 발생하더라도 중지되지 않고 나머지 내용을 페이지에 출력합니다.
    • 일반적인 페이지 출력을 위해서 사용되며, include 문장을 만날 때 호출이 됩니다.