php

외부 파일을 포함하는 함수 [include(), include_once(), require(), require_once()]

말랑Cow 2021. 7. 8. 00:00
반응형

여러 파일에 공통적으로 사용하는 코드는 별도의 파일로 만든 후 각 파일에서 불러오는 것이 좋습니다.

코드의 양이 줄어들고 수정이 용이하기 때문입니다.

 

● include

- 같은 파일 여러 번 포함 가능

- 포함할 파일이 없어도 다음 코드 실행

 

● include_once

- 같은 파일 한 번만 포함

- 포함할 파일이 없어도 다음 코드 실행

 

● require

- 같은 파일 여러 번 포함 가능

- 포함할 파일이 없으면 다음 코드 실행하지 않음

 

● require_once

- 같은 파일 한 번만 포함

- 포함할 파일이 없으면 다음 코드 실행하지 않음

 

몇 가지 테스트로 확인해 보겠습니다.

 

[root@localhost test2]# cat include.php include_once.php require.php require_once.php
<h1> include test </h1>
<h1> include once test </h1>
<h1> require test </h1>
<h1> require once test </h1>

각 파일명과 파일의 내용들 입니다.

 

1. include 


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> TEST </title>
</head>

<body>
<?php
 include 'include.php';
?>

<h2> END </h2>

</body>
</html>

 

 

잘 확인되었습니다. 

 

이번엔 중복으로 포함시켜 보겠습니다.

 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> TEST </title>
</head>

<body>
<?php
 include 'include.php';
 include 'include.php';

?>

<h2> END </h2>

</body>
</html>

중복되어 가져오는 것을 확인할 수 있습니다.

 

이번엔 없는 파일을 포함시켜 보겠습니다.

 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> TEST </title>
</head>

<body>
<?php
 include 'sjh_include.php';
?>

<h2> END </h2>

</body>
</html>

없는 파일을 포함시켜도 다음 코드가 잘 실행됩니다.

 

2. include_once


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> TEST </title>
</head>

<body>
<?php
 include_once 'include_once.php';
 include_once 'include_once.php';
?>

<h2> END </h2>

</body>
</html>

include_once 로 두번 포함시켰지만 한번만 포함되는 것을 확인할 수 있습니다.

 

동일하게 없는 파일을 포함시켜 보겠습니다.

 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> TEST </title>
</head>

<body>
<?php
 include_once 'sjh_include_once.php';
?>

<h2> END </h2>

</body>
</html>

역시나 다음 코드가 실행이 잘 됩니다.

 

3. require


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> TEST </title>
</head>

<body>
<?php
 require 'require.php';
 require 'require.php';
?>

<h2> END </h2>

</body>
</html>

동일한 파일을 두 번 포함시킬 수 있는 것을 확인할 수 있습니다.

 

다음은 없는 파일을 포함하겠습니다.

 

!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> TEST </title>
</head>

<body>
<?php
 require 'sjh_require.php';
?>

<h2> END </h2>

</body>
</html>

500 error 가 발생하며 다음 코드( <h2> END </h2> ) 가 실행되지 않는 것을 확인할 수 있습니다.

 

4. require_once


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> TEST </title>
</head>

<body>
<?php
 require_once 'require_once.php';
 require_once 'require_once.php';
?>

<h2> END </h2>

</body>
</html>

같은 파일을 두 번 포함시켰지만 한번만 포함되었습니다.

 

마찬가지로 없는 파일을 포함시켜보면

 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> TEST </title>
</head>

<body>
<?php
 require_once 'sjh_require_once.php';
?>

<h2> END </h2>

</body>
</html>

동일한 500에러를 뿜으며 다음코드가 실행되지 않습니다.

 

이번엔 require 와 require_once를 각각 써서 같은 파일을 포함시켜 보겠습니다.

 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> TEST </title>
</head>

<body>
<?php
 require_once 'require_once.php';
 require 'require_once.php'
?>

<h2> END </h2>

</body>
</html>

 

require_once 는 require로 포함하는 것에는 영향을 미치지 않습니다.

 

 

반응형