3.09. 다른 파일 포함하기

php는 다른 php 파일을 불러와서 현재 파일의 일부인 것처럼 포함시켜 다룰 수 있다.

<?php
$var = "abcd";

echo "this is before page : ";
echo $var;
echo "<br />";

위 파일을 before.php 로 저장한다.


<?php
echo "this is after page : ";
echo $var;
echo "<br />";

위 파일을 after.php 로 저장한다.


<?php
require("before.php");

echo "this is main page : ";
echo $var;
echo "<br />";

require_once("after.php");

위 파일을 main.php 로 저장한다.


http://localhost/main.php 에서 결과를 확인한다.

this is before page : abcd
this is main page : abcd
this is after page : abcd

main.php 페이지가 실행되면 먼저 before.php 파일을 불러온다.

require("before.php");

이후 main.php의 메세지를 출력한다.

echo "this is main page : ";
echo $var;
echo "<br />";

마지막으로 after.php 파일을 불러온다.

require_once("after.php");

before.php 에서 선언한 변수 $var  after.php 에서 사용할 수 있는 것을 확인할 수 있다.

echo "this is after page : ";
echo $var;

php 에서 다른 php 파일을 불러올 때 쓰는 명령어는 include, include_once, require, require_once 4개가 있다.

  • include 는 파일이 없어도 경고만 나올 뿐 PHP는 계속 동작한다. 파일 하나가 여러번 호출되면 호출되는 횟수만큼 파일을 포함시킨다.
  • include_once 는 파일이 없어도 경고만 나올 뿐 PHP는 계속 동작한다. 파일 하나가 여러번 호출되어도 처음 한번만 파일을 불러온다.
  • require 는 파일이 없으면 오류가 나며 PHP의 실행이 완전히 멈춘다. 파일 하나가 여러번 호출되면 호출되는 횟수만큼 파일을 포함시킨다.
  • require_once는 파일이 없으면 오류가 나며 PHP의 실행이 완전히 멈춘다. 파일 하나가 여러번 호출되어도 처음 한번만 파일을 불러온다.

include의 예제를 확인한다.

inc.php

echo "inc 파일 호출됨 <br />";

main_inc.php

include("inc.php");
include("inc.php");

include("no_inc.php");

echo "main_inc 끝";

http://localhost/main_inc.php 를 실행했을 때 결과는 아래와 같다.

inc 파일 호출됨
inc 파일 호출됨

warning : include(no_inc.php) [function.include] : failed to open stream

main_inc 끝

같은 inc.php 파일이 두 번 호출되었다.

include("inc.php");
include("inc.php");
inc 파일 호출됨
inc 파일 호출됨

파일이 없어도 프로그램은 여전히 실행된다.

include("no_inc.php");

echo "main_inc 끝";
warning : include(no_inc.php) [function.include] : failed to open stream

main_inc 끝

include_once의 예제를 확인한다.

main_inc_once.php

include_once("inc.php");
include_once("inc.php");

http://localhost/main_inc_once.php 를 실행했을 때 결과는 아래와 같다.

inc 파일 호출됨

여러번 inc.php를 include_once 했지만 한번만 불러왔다.


require의 예제를 확인한다.

main_req.php

require("inc.php");
require("inc.php");

require("no_inc.php");

같은 inc.php 파일이 두 번 호출되었다. 결과는 아래와 같다.

inc 파일 호출됨
inc 파일 호출됨

파일이 없으면 오류가 난다.

require("no_inc.php");

echo "main_inc 끝";
Fatal error : require(no_inc.php) [function.require] : failed opening required


main_inc 끝 메세지가 출력되지 않음을 확인하자.


require_once의 예제를 확인한다.

main_req_once.php

require_once("inc.php");
require_once("inc.php");
inc 파일 호출됨

php에서 require 는 단순히 다른 파일을 내 파일의 일부처럼 가지고 온다는 뜻이다. 불러오는 순간 전역변수와 함수와 클래스등 모든 것이 현재 스택이 존재하는 스코프 안에서 동작한다.


만약 require_once 혹은 include_once 를 사용할 때 만약 함수 내부 등 제한된 스코프에서 사용되면 불러온 변수 등은 현재 스코프가 끝나는 순간 없어진다. 만약 함수가 한번 더 호출되어도 _once 여서 다시 한번 include  require 가 동작하지 않기 때문에 변수가 현재 스코프에서 불러지지 않아 오류가 나는 경우가 있으므로 유의해야 한다.

inc_val.php

$inc_val = "inc 변수";

require_scope.php

function require_scope(){
    require_once("inc_val.php");
    echo $inc_val;
}

require_scope();
require_scope();

require_scope.php 파일을 실행시켜 보면 아래와 같은 오류가 나온다.

Copyinc 변수
Notice: Undefined variable: inc_val in