PHP allows you to insert the content of one PHP file into another using include and require. This promotes code reuse and keeps your project organized.
The key difference: if the file is not found, include produces a warning and continues execution, while require produces a fatal error and stops execution.
<?php
// include - warning if file missing, script continues
include 'header.php';
include 'nav.php';
// require - fatal error if file missing, script stops
require 'config.php'; // critical - must exist
require 'database.php'; // critical - must exist
// include_once / require_once - prevent duplicate inclusion
include_once 'functions.php';
require_once 'classes/User.php';
echo "Page content here";
include 'footer.php';
?>
A common use case is splitting a page into reusable parts: header, navigation, content, and footer.
<!-- header.php -->
<?php
$siteName = "My Website";
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $siteName; ?></title>
</head>
<body>
<header><h1><?php echo $siteName; ?></h1></header>
<?php
require_once 'includes/header.php';
require_once 'includes/nav.php';
?>
<main>
<h2>Welcome to the Home Page</h2>
<p>This is the main content area.</p>
</main>
<?php
require_once 'includes/footer.php';
?>
<!-- config.php - loaded once, shared across all pages -->
<?php
// config.php
define('DB_HOST', 'localhost');
define('DB_NAME', 'myapp');
define('DB_USER', 'root');
define('DB_PASS', 'secret');
define('BASE_URL', 'https://example.com/');
?>
Explore 500+ free tutorials across 20+ languages and frameworks.