본문 바로가기

[Terry] PHP

5. PHP 기본4

 include

include 되는 문서의 소스를 그대로 가져오는 것으로, HTML 소스 이외에도 선언된 변수나 함수 등을 그대로 가져온다.

형식

include "포함할 문서";

예제

 test1.php

  1. <?
  2. $a = "this variable is in test1.php";

  3. function test(){

  4. echo "You've called function test()";

  5. }

  6. ?>

test2.php

  1. <?
  2.  include "test1.php";

  3. echo $a;

  4. test();

  5. ?>
결과값

this variable is in test1.php

You've called function test()

 

require

include와 비슷한 기능을 한다.

형식

 require "포함할 문서";

차이점

include require
 a.php에서 b.php를 include했을 경우 PHP는 a.php와 b.php를 따로 해석한다. b.php를 a.php의 일부분으로 생각하여 한번에 해석한다.
 포함된 문서를 기준으로 에러 위치를 알려준다.

포함하는 문서를 기준으로 에러 위치를 알려준다.

 require 보다는 include를 사용하는 것을 권장한다. 이유는 에러위치를 찾는데 include가 더 좋기 때문이다.

 

 

 

 

이 글은 스프링노트에서 작성되었습니다.