PHP Include Files

PHP Include Files allow you to reuse code by inserting the contents of one PHP file into another. This is especially useful for common elements like headers, footers, or configuration files. PHP provides several ways to include files:


Basic Syntax

include 'filename.php';

Or

require 'filename.php';

Difference Between include and require

Function Behavior if File is Missing
include Shows a warning, script continues
require Shows a fatal error, script stops

Example Usage

header.php

<!DOCTYPE html> <html> <head> <title>My Website</title> </head> <body> 

footer.php

</body> </html> 

index.php

<?php include 'header.php'; ?> <h1>Welcome to my website!</h1> <?php include 'footer.php'; ?>

Other Variants

  • include_once 'file.php'; – Includes the file only once, avoids re-inclusion.

  • require_once 'file.php'; – Same as above, but stops execution on failure.