PHP include vs require
ITWeb/개발일반 2007. 8. 22. 14:44include
- include_path 를 먼저 찾는다
- a.php 내부에 b.php 가 include 되어 있으면 b.php 를 먼저 찾는다.
- include 실패시 warning 을 내고 script 는 진행이 되지만 require 는 fatal error 를 내고 정지 한다.
- include 는 변수 scope 을 그대로 유지 한다.
- function, class, defined 는 global scope 을 갖는다.
The documentation below also applies to require(). The two constructs 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, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. Be warned that parse error in included file doesn't cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does.
Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/. If filename begins with ./ or ../, it is looked only in the current working directory.
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.
require
- 실행중인 processing 을 중지 하고 싶을 때 사용한다.
- 상속을 사용하지 않을 때 사용한다.
- looping 구조에 어떠한 행동도 하지 않는다.
- loop 안에서 한번만 발생된다.
require() will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.
require_once
- 이미 include 되어 있다면 다시 include 하지 않는다.
- require 와 비슷하다.
include_once
- 이미 include 되어 있다면 다시 include 하지 않는다.
- include 와 비슷하다.
reference : http://au.php.net/manual/en/function.include.php