← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 19 of 24

PHP Include & Require

include and require pull another file's code into your page, like adding a chapter into your book. The difference is that require stops everything if the file is missing, while include only warns.
Syntax
php
include "file.php";        // warning if missing, script continues
require "file.php";        // fatal error if missing
include_once "file.php";   // skip if already included
require_once "file.php";

What Do Include and Require Do?

include and require both pull the contents of another PHP file into the current script at the point they're called, letting you split code across files instead of writing one giant script.

This is the classic mechanism PHP used for modularity long before Composer autoloading existed.

Example: What Do Include and Require Do?

php
<?php
// Call `file_put_contents("helper.php", "<?php function greet() { echo 'Hello!'; }")`
file_put_contents("helper.php", "<?php function greet() { echo 'Hello!'; }");
// Load and run the file `helper.php`
include "helper.php";
// Call `greet()`
greet();
?>

include vs require

The only real difference is what happens when the target file is missing: include emits a warning and the script keeps running, while require raises a fatal error and execution stops immediately.

Use require for files the application genuinely cannot function without, like a core config file.

Example: include vs require

php
<?php
// Declare `$result`, set to `@include "missing_file.php"`
$result = @include "missing_file.php";
// Print `$result === false ? "include: warning only, script continues" : "included"` to the output
echo $result === false ? "include: warning only, script continues" : "included";
?>

The _once Variants

include_once and require_once check whether the file has already been included anywhere in the current request and skip it silently if so, which prevents the "cannot redeclare function/class" fatal errors that happen when the same file gets pulled in twice through different code paths.

Example: The _once Variants

php
<?php
file_put_contents("helper.php", "<?php function greet() { echo 'Hello!'; }");
require_once "helper.php";
require_once "helper.php"; // safely skipped the second time
greet();
?>

Sharing Variable Scope

An included file runs in the exact variable scope of the line that included it — if you include a file from inside a function, that file can only see the function's local variables, not the global scope, unless you explicitly pass data in.

Example: Sharing Variable Scope

php
<?php
// Call `file_put_contents("show.php", "<?php echo \$localVar;")`
file_put_contents("show.php", "<?php echo \$localVar;");
// Define the function `useInclude` with no parameters
function useInclude() {
    // Declare `$localVar`, set to "visible inside function"
    $localVar = "visible inside function";
    // Load and run the file `show.php`
    include "show.php";
}
// Call `useInclude()`
useInclude();
?>

Include/Require vs Autoloading

For simple scripts, include/require are straightforward and explicit.

For class-heavy applications, Composer's PSR-4 autoloading (covered elsewhere) replaces manual includes entirely by loading class files automatically the first time a class name is referenced.

Example: Include/Require vs Autoloading

php
<?php
// require 'User.php'; require 'Product.php'; ... (manual)
// vs Composer's PSR-4 autoloading (automatic, class-based)
echo "Manual includes work but don't scale like autoloading does";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using include for a file that is required to run, so the script continues after a warning; use require for critical files.
  2. Including the same file twice and getting redeclared function errors, when include_once avoids that.
  3. Using a relative path that depends on the working directory, so the file is not found; build the path with __DIR__.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.