PHP 15강. include와 require
1. include란?
include는 다른 PHP 파일을 현재 파일 안으로 불러오는 기능입니다.
예를 들어
Header
Footer
메뉴
공통 함수
등을 한 번만 만들어 여러 페이지에서 사용할 수 있습니다.
2. 왜 사용할까?
예를 들어
Home
Course
Gallery
Notice
Reservation
모든 페이지에 같은 Header가 있다면
각 페이지마다 작성하는 대신
Header 파일 하나만 만들어 불러옵니다.
3. 프로젝트 구조
dongbusan
├── index.php
├── reservation.php
├── notice.php
├── gallery.php
│
├── includes
│ ├── header.php
│ ├── footer.php
│ └── nav.php
│
├── config
│ └── db.php
│
├── css
├── js
└── images
4. Header 만들기
header.php
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>동부산CC</title>
<link rel="stylesheet"
href="css/style.css">
</head>
<body>
<header>
<h1>동부산CC</h1>
</header>
5. include 사용
index.php
<?php
include "includes/header.php";
?>
<h2>메인페이지</h2>
<?php
include "includes/footer.php";
?>
6. Footer 만들기
footer.php
<footer>
Copyright 2026
Dongbusan CC
</footer>
</body>
</html>
7. Navigation 분리
nav.php
<nav>
<a href="index.php">홈</a>
<a href="course.php">코스</a>
<a href="reservation.php">예약</a>
<a href="notice.php">공지</a>
</nav>
필요한 페이지마다
include "includes/nav.php";
만 작성하면 됩니다.
8. require
require "config/db.php";
require는 반드시 필요한 파일을 불러올 때 사용합니다.
9. include와 require 차이
include require
파일이 없어도 경고 후 계속 실행 파일이 없으면 즉시 종료
선택 기능 필수 기능
10. DB 연결
config/db.php
<?php
$pdo = new PDO(...);
사용
<?php
require "config/db.php";
DB 연결은 프로젝트 전체에서 하나만 관리하는 것이 좋습니다.
11. 공통 함수
functions.php
<?php
function hello(){
return "동부산CC";
}
사용
include "functions.php";
echo hello();
12. 동부산CC 예제
Header
↓
Menu
↓
본문
↓
Footer
모든 페이지가 동일한 구조를 사용합니다.
13. 관리자 페이지
admin
↓
header.php
↓
sidebar.php
↓
content.php
↓
footer.php
관리자 화면도 파일을 분리하여 관리합니다.
14. React와 비교
React
<Header/>
<Footer/>
PHP
include "header.php";
include "footer.php";
둘 다 공통 UI를 재사용하는 개념입니다.
15. include_once
include_once "header.php";
같은 파일을 여러 번 불러와도 한 번만 실행됩니다.
16. require_once
require_once "config/db.php";
DB 연결 파일처럼 반드시 한 번만 실행되어야 하는 파일에 사용합니다.
17. 실무에서 자주 분리하는 파일
header.php
footer.php
nav.php
sidebar.php
db.php
functions.php
auth.php
18. 동부산CC 프로젝트 적용
index.php
↓
header.php
↓
nav.php
↓
본문
↓
footer.php
모든 페이지가 같은 레이아웃을 사용합니다.
19. 실습 과제
다음 파일을 만들어 보세요.
header.php
footer.php
nav.php
그리고
index.php
reservation.php
notice.php
에서 include를 이용해 공통 레이아웃을 적용해 보세요.
20. 정리
include와 require는 공통 파일을 여러 페이지에서 재사용하기 위한 기능입니다.
include는 선택적으로 불러오는 파일에 적합합니다.
require는 데이터베이스 연결처럼 반드시 필요한 파일에 적합합니다.
include_once, require_once는 같은 파일이 중복 실행되는 것을 방지합니다.
동부산CC 프로젝트에서는 Header, Footer, 메뉴, DB 연결, 공통 함수 등을 분리하면 코드가 훨씬 깔끔해지고 유지보수가 쉬워집니다.
1. include란?
include는 다른 PHP 파일을 현재 파일 안으로 불러오는 기능입니다.
예를 들어
Header
Footer
메뉴
공통 함수
등을 한 번만 만들어 여러 페이지에서 사용할 수 있습니다.
2. 왜 사용할까?
예를 들어
Home
Course
Gallery
Notice
Reservation
모든 페이지에 같은 Header가 있다면
각 페이지마다 작성하는 대신
Header 파일 하나만 만들어 불러옵니다.
3. 프로젝트 구조
dongbusan
├── index.php
├── reservation.php
├── notice.php
├── gallery.php
│
├── includes
│ ├── header.php
│ ├── footer.php
│ └── nav.php
│
├── config
│ └── db.php
│
├── css
├── js
└── images
4. Header 만들기
header.php
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>동부산CC</title>
<link rel="stylesheet"
href="css/style.css">
</head>
<body>
<header>
<h1>동부산CC</h1>
</header>
5. include 사용
index.php
<?php
include "includes/header.php";
?>
<h2>메인페이지</h2>
<?php
include "includes/footer.php";
?>
6. Footer 만들기
footer.php
<footer>
Copyright 2026
Dongbusan CC
</footer>
</body>
</html>
7. Navigation 분리
nav.php
<nav>
<a href="index.php">홈</a>
<a href="course.php">코스</a>
<a href="reservation.php">예약</a>
<a href="notice.php">공지</a>
</nav>
필요한 페이지마다
include "includes/nav.php";
만 작성하면 됩니다.
8. require
require "config/db.php";
require는 반드시 필요한 파일을 불러올 때 사용합니다.
9. include와 require 차이
include require
파일이 없어도 경고 후 계속 실행 파일이 없으면 즉시 종료
선택 기능 필수 기능
10. DB 연결
config/db.php
<?php
$pdo = new PDO(...);
사용
<?php
require "config/db.php";
DB 연결은 프로젝트 전체에서 하나만 관리하는 것이 좋습니다.
11. 공통 함수
functions.php
<?php
function hello(){
return "동부산CC";
}
사용
include "functions.php";
echo hello();
12. 동부산CC 예제
Header
↓
Menu
↓
본문
↓
Footer
모든 페이지가 동일한 구조를 사용합니다.
13. 관리자 페이지
admin
↓
header.php
↓
sidebar.php
↓
content.php
↓
footer.php
관리자 화면도 파일을 분리하여 관리합니다.
14. React와 비교
React
<Header/>
<Footer/>
PHP
include "header.php";
include "footer.php";
둘 다 공통 UI를 재사용하는 개념입니다.
15. include_once
include_once "header.php";
같은 파일을 여러 번 불러와도 한 번만 실행됩니다.
16. require_once
require_once "config/db.php";
DB 연결 파일처럼 반드시 한 번만 실행되어야 하는 파일에 사용합니다.
17. 실무에서 자주 분리하는 파일
header.php
footer.php
nav.php
sidebar.php
db.php
functions.php
auth.php
18. 동부산CC 프로젝트 적용
index.php
↓
header.php
↓
nav.php
↓
본문
↓
footer.php
모든 페이지가 같은 레이아웃을 사용합니다.
19. 실습 과제
다음 파일을 만들어 보세요.
header.php
footer.php
nav.php
그리고
index.php
reservation.php
notice.php
에서 include를 이용해 공통 레이아웃을 적용해 보세요.
20. 정리
include와 require는 공통 파일을 여러 페이지에서 재사용하기 위한 기능입니다.
include는 선택적으로 불러오는 파일에 적합합니다.
require는 데이터베이스 연결처럼 반드시 필요한 파일에 적합합니다.
include_once, require_once는 같은 파일이 중복 실행되는 것을 방지합니다.
동부산CC 프로젝트에서는 Header, Footer, 메뉴, DB 연결, 공통 함수 등을 분리하면 코드가 훨씬 깔끔해지고 유지보수가 쉬워집니다.
#PHP 15강. include와 require