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 script
  • include 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 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

<html>
<body>

<h1>Welcome to my home page!</h1>
<?php include 'noFileExists.php';
echo "I have a $color $car.";
?>

</body>
</html>

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

<html>
<body>

<h1>Welcome to my home page!</h1>
<?php require 'noFileExists.php';
echo "I have a $color $car.";
?>

</body>
</html>

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.



source - http://www.w3schools.com/php/php_includes.asp








다른 파일을 불러들인다는 점에서 include와 require는 같다. 하지만 오류가 있을 경우 그걸 처리하는 방식이 다르다.

include는 오류가 있으면 오류 내용을 경고로 알린 뒤, 그 나머지 부분을 제대로 실행한다. 반면 require는 오류가 있으면 오류 내용을 알린 뒤 그걸로 끝이다. 일단 오류가 일어나면 더 이상 프로그램을 실행하지 않는다.

include의 오류 예

예를 들어, 아래와 같은 코드를 생각해 보자.


sample.php

<html>
<body>

<?php
echo "<p>안녕하신감</p>";
include "../footer.php";
echo "<p>안녕 못하당</p>";
?>

<p>안 됐당</p>

</body>
</html>

해당 경로에 footer.php 파일이 없을 경우 sample.php 파일은 브라우저에 다음과 같은 결과를 보여준다.

안녕하신감

Warning: main(../footer.php): failed to open stream: No such file or directory in /example/sample.php on line 6

Warning: main(../footer.php): failed to open stream: No such file or directory in /example/sample.php on line 6

Warning: main(): Failed opening '../footer.php' for inclusion (include_path='.:/example') in /example/sample.php on line 6

안녕 못하당

안 됐당

오류 내용에서 경로명은 임의로 바꾼 것이고, 하여튼 중요한 건 오류가 있는 부분은 실행되지 않지만 앞뒤로 그 나머지는 모두 실행된다는 점이다.

require의 오류 예

하지만 require의 경우엔 다르다. 예를 들어, 아래와 같은 코드를 짰다고 하자.


sample.php

<html>
<body>

<?php
echo "<p>안녕하신감</p>";
require "../footer.php";
echo "<p>안녕 못하당</p>";
?>

<p>안 됐당</p>

</body>
</html>

그런데 해당 경로에 footer.php 파일이 없다면 sample.php 파일은 브라우저에 다음과 같은 결과를 보여준다.

안녕하신감

Warning: main(../footer.php): failed to open stream: No such file or directory in /example/sample.php on line 6

Warning: main(../footer.php): failed to open stream: No such file or directory in example/sample.php on line 6

Fatal error: main(): Failed opening required '../footer.php' (include_path='.:/example') in /example/sample.php on line 6

include 때와는 달리 '안녕 못하당'과 '안 됐당'이 출력되지 않았다. 그러니까 require는 오류가 있으면 거기부터 그 나머지는 실행하지 않는다는 것을 알 수 있다. 게다가 오류도 include에서는 그냥 경고 (Warning)라고만 하는데, require에서는 치명적인 오류 (Fatal error)라고 한다.




'Development > PHP' 카테고리의 다른 글

php - mail  (0) 2013.04.06
copyright 년도 자동표기하기  (0) 2013.03.06
php - date() 오류  (0) 2012.07.21
php - mail 함수  (1) 2012.07.11
PHP Framework - CodeIgniter  (0) 2012.05.07
Posted by linuxism
,