Real websites are never one giant PHP file — headers, footers, and reusable logic get split into their own files and pulled in wherever they're needed. PHP has four keywords for this, and the differences between them matter.
Both pull the contents of another PHP file into the current one, as if it had been typed there directly:
// header.php
<?php
$siteName = "Learn Computer Academy";
?>
<header><h1><?php echo $siteName; ?></h1></header>// index.php
<?php
include "header.php";
?>
<p>Page content goes here.</p>The difference is what happens if the file is missing: include raises a warning and the script keeps running; require raises a fatal error and stops the script entirely.
| Keyword | If the file is missing | Use for |
|---|---|---|
| include | Warning, script continues | Optional pieces — the page still works without it |
| require | Fatal error, script stops | Essential pieces — the page is broken without it (like a database connection file) |
include_once and require_once do the same job, but skip re-including a file that's already been included once during this request — essential for files that define a function or class, since defining the same function twice is a fatal error:
<?php
require_once "db-connection.php";
require_once "db-connection.php"; // silently skipped, no error
?>A practical default
As a practical default: use require_once for anything defining functions or classes, and plain include for optional display fragments like a promotional banner. This single rule covers the vast majority of real cases.