반응형
말랑Cow
IT
말랑Cow
전체 방문자
오늘
어제
  • 분류 전체보기 (165)
    • Linux (33)
    • Windows (6)
    • Network (7)
    • Hardware (10)
    • Zabbix (13)
    • python (9)
    • script (0)
    • docker (16)
    • NAS (1)
    • DB (3)
    • php (33)
    • jQuery (1)
    • 정보보안기사 (0)
    • Ajax (1)
    • Javascript (21)
    • DELL (0)
    • HPE (0)
    • Secui (0)
    • AWS (2)
    • Elastic (0)
    • 보안 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Shell
  • 서버
  • 모니터링
  • CentOS
  • 리눅스
  • 파이썬
  • php 객체
  • php
  • Linux
  • docker
  • ZABBIX
  • bash
  • 네트워크
  • jQuery
  • 도커
  • JavaScript
  • 자빅스
  • Python
  • 자바스크립트
  • php 클래스
hELLO · Designed By 정상우.
말랑Cow

IT

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

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

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로 포함하는 것에는 영향을 미치지 않습니다.

 

 

반응형

'php' 카테고리의 다른 글

selectbox 에서 선택한 값 출력  (0) 2021.07.10
양식(form)으로 전송된 데이터 전달받기 - $_GET, $_POST  (0) 2021.07.09
__FILE__ 과 __DIR__  (0) 2021.07.07
PHP Mysql 레코드 가져오기  (0) 2021.07.07
PHP MySQL 연결 및 쿼리실행  (0) 2021.07.06
    'php' 카테고리의 다른 글
    • selectbox 에서 선택한 값 출력
    • 양식(form)으로 전송된 데이터 전달받기 - $_GET, $_POST
    • __FILE__ 과 __DIR__
    • PHP Mysql 레코드 가져오기
    말랑Cow
    말랑Cow

    티스토리툴바