📅 Week 10 - Day 2: Includes & File Structure

Wednesday | Duration: 3 hours

🎯 Learning Objectives

📂 Include vs Require

Statement Description On Error
include Includes file, continues if file not found Warning (script continues)
include_once Includes file only once Warning (script continues)
require Requires file, stops if file not found Fatal error (script stops)
require_once Requires file only once Fatal error (script stops)

🏗️ Project File Structure

my-website/ │ ├── includes/ │ ├── header.php │ ├── footer.php │ ├── nav.php │ └── config.php │ ├── css/ │ └── style.css │ ├── images/ │ └── logo.png │ ├── index.php ├── about.php ├── contact.php └── services.php

📝 Creating Reusable Components

1. Header Component (includes/header.php)

<!-- includes/header.php --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php echo $pageTitle; ?></title> <link rel="stylesheet" href="css/style.css"> </head> <body> <header> <div class="container"> <h1>My Website</h1> <p>Welcome to our amazing site</p> </div> </header>

2. Navigation Component (includes/nav.php)

<!-- includes/nav.php --> <nav> <ul> <li><a href="index.php">Home</a></li> <li><a href="about.php">About</a></li> <li><a href="services.php">Services</a></li> <li><a href="contact.php">Contact</a></li> </ul> </nav>

3. Footer Component (includes/footer.php)

<!-- includes/footer.php --> <footer> <div class="container"> <p>© 2025 My Website. All rights reserved.</p> <p>Built with PHP</p> </div> </footer> </body> </html>

4. Config File (includes/config.php)

<?php // includes/config.php // Site configuration define('SITE_NAME', 'My Website'); define('SITE_EMAIL', 'info@mywebsite.com'); define('SITE_PHONE', '080-123-4567'); // Database configuration define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASS', ''); define('DB_NAME', 'my_database'); // Paths define('BASE_URL', 'http://localhost/my-website/'); define('UPLOAD_DIR', 'uploads/'); ?>

🔗 Using Includes in Pages

Homepage (index.php)

<?php $pageTitle = "Home - My Website"; include 'includes/header.php'; include 'includes/nav.php'; ?> <main> <div class="container"> <h2>Welcome to Our Homepage</h2> <p>This is the main content of the homepage.</p> </div> </main> <?php include 'includes/footer.php'; ?>

About Page (about.php)

<?php $pageTitle = "About Us - My Website"; include 'includes/header.php'; include 'includes/nav.php'; ?> <main> <div class="container"> <h2>About Us</h2> <p>Learn more about our company and mission.</p> </div> </main> <?php include 'includes/footer.php'; ?>

💻 Complete Modular Website Example

<!-- includes/header.php --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo isset($pageTitle) ? $pageTitle : 'My Website'; ?></title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: Arial, sans-serif; line-height: 1.6; } .container { max-width: 1200px; margin: 0 auto; padding: 20px; } header { background: #333; color: white; padding: 20px 0; text-align: center; } nav { background: #555; padding: 15px 0; } nav ul { list-style: none; text-align: center; } nav ul li { display: inline; margin: 0 15px; } nav ul li a { color: white; text-decoration: none; padding: 10px 20px; } nav ul li a:hover { background: #777; } main { min-height: 400px; padding: 40px 0; } footer { background: #333; color: white; text-align: center; padding: 20px 0; margin-top: 40px; } </style> </head> <body> <header> <div class="container"> <h1>Welcome to My Website</h1> </div> </header> <nav> <ul> <li><a href="index.php">Home</a></li> <li><a href="about.php">About</a></li> <li><a href="services.php">Services</a></li> <li><a href="contact.php">Contact</a></li> </ul> </nav>
<!-- includes/footer.php --> <footer> <div class="container"> <p>© <?php echo date("Y"); ?> My Website. All rights reserved.</p> <p>Contact: info@mywebsite.com | Phone: 080-123-4567</p> </div> </footer> </body> </html>
<?php // index.php $pageTitle = "Home - My Website"; include 'includes/header.php'; ?> <main> <div class="container"> <h2>Welcome to Our Homepage</h2> <p>This is a modular PHP website with reusable components.</p> <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin-top: 30px;"> <div style="background: #f4f4f4; padding: 20px; border-radius: 5px;"> <h3>Feature 1</h3> <p>Amazing feature description here.</p> </div> <div style="background: #f4f4f4; padding: 20px; border-radius: 5px;"> <h3>Feature 2</h3> <p>Another great feature description.</p> </div> <div style="background: #f4f4f4; padding: 20px; border-radius: 5px;"> <h3>Feature 3</h3> <p>One more awesome feature here.</p> </div> </div> </div> </main> <?php include 'includes/footer.php'; ?>

🎨 Dynamic Active Navigation

<?php // Get current page filename $currentPage = basename($_SERVER['PHP_SELF']); ?> <nav> <ul> <li> <a href="index.php" class="<?php echo ($currentPage == 'index.php') ? 'active' : ''; ?>"> Home </a> </li> <li> <a href="about.php" class="<?php echo ($currentPage == 'about.php') ? 'active' : ''; ?>"> About </a> </li> <li> <a href="contact.php" class="<?php echo ($currentPage == 'contact.php') ? 'active' : ''; ?>"> Contact </a> </li> </ul> </nav> <style> nav a.active { background: #777; font-weight: bold; } </style>

✏️ Practice Exercises

  1. Create a modular website with header, nav, and footer
  2. Build at least 4 pages (home, about, services, contact)
  3. Create a config.php file with site settings
  4. Implement dynamic page titles
  5. Add active navigation highlighting

✅ Best Practices

✅ Today's Deliverable

← Previous Course Home Next: Day 3 →