include 'header.php'; runs the code of another PHP file at that point in the script, as if it were pasted there. require 'header.php'; does the same, but if the file cannot be loaded, require stops the script with a fatal error while include only prints a warning and continues. The usual first use is a header and footer shared by every page.
The block writes header.php and footer.php first, so there are files to include. Run it and look at the Page tab:
The included files are ordinary PHP files: they can contain HTML with <?= ?> tags, and they see the $title variable set before the require. Change the title and run it again; every page that requires the header would pick up a change to that one file.
include vs require
The only difference is what happens when the file is missing or unreadable. include prints two warnings, evaluates to false, and the script continues:
Warning: include(sidebar.php): Failed to open stream: No such file or directory in /home/index.php on line 2
Warning: include(): Failed opening 'sidebar.php' for inclusion (include_path='.:/usr/share/php83') in /home/index.php on line 2
require prints the same warning and then throws an Error, which ends the script unless something catches it:
PHP Warning: require(config.php): Failed to open stream: No such file or directory in /home/index.php on line 2
PHP Fatal error: Uncaught Error: Failed opening required 'config.php' (include_path='.:/usr/share/php83') in /home/index.php:2
So use require for anything the page cannot run without (configuration, function libraries, class files) and include for optional parts. For a part that may legitimately be absent, check for it first rather than relying on the warning:
Output:
Spring sale: 20% off
banner.php included, include returned int(1)
sidebar.php not found, skipped
include_once and require_once
A file that defines a function or a class can only be loaded once: loading it a second time is a fatal error, because the function already exists.
Fatal error: Cannot redeclare greet() (previously declared in /home/helpers.php:2) in /home/helpers.php on line 2
That happens as soon as two files each require 'helpers.php'. require_once and include_once remember which files were already loaded and skip them the second time, returning true:
Output:
int(1)
bool(true)
bool(true)
Hello, Ada
Use the _once forms for files that declare things, and plain include for templates you want to output more than once. In projects that use Composer, the autoloader loads class files for you, so require_once calls for classes mostly disappear; see Composer.
Return a value from an included file
A return at the top level of an included file stops that file and hands a value back to the include or require expression. That makes a clean configuration file: no global variables, just a value you assign to whatever name you like.
Output:
My Shop
shop
bool(false)
Laravel's configuration files (config/app.php, config/database.php and the rest) work this way.
Variables and scope in included files
Included code runs in the scope of the line that includes it. At the top of a script it sees every global variable; inside a function it sees only that function's local variables. That second rule is how a tiny template renderer works: a function whose locals are exactly the variables the template should see.
The Page tab shows both lists, rendered from one template file. Because include runs inside render(), the template sees $heading and $products but not $secret. The foreach ... endforeach form used in the template is the alternative syntax PHP offers for HTML templates.
Include paths and DIR
A path like 'helper.php' is not resolved relative to the file that contains the include. PHP searches the include_path setting (which normally starts with ., the current working directory) and only then the including file's own folder. So inside partials/nav.php, include 'helper.php' can load a completely different helper.php from the project root. __DIR__, the directory of the current file, removes the guesswork:
Output:
loaded the root helper.php
loaded partials/helper.php
__DIR__ in nav.php is partials
Write every include as __DIR__ . '/relative/path.php'. The working directory depends on how the script was started (the web server, a cron job, the command line from another folder), but __DIR__ never changes.
Never include a file named by the user
A common first router reads the page name from the URL: include $_GET['page'] . '.php';. That lets a visitor choose which file on the server runs, for example ?page=../../config (local file inclusion), and with old settings even a file from another server. Map allowed names to files instead. Run the block and click the links: each click runs the script again with a different $_GET['page'].
Only the three names in $pages can ever reach require; the ../secret link gets the "Not found" page. Reading request values safely is covered in _POST.
Frequently Asked Questions
What is the difference between include and require in PHP?
Both load and run another PHP file. If the file cannot be loaded, require stops the script with a fatal error, while include prints a warning, returns false and lets the script continue. Use require for files the page cannot work without, such as configuration and function libraries.
When should I use require_once instead of require?
Use require_once for files that define functions, classes or constants. Loading such a file twice is a fatal error (Cannot redeclare), and require_once skips the file if it has already been loaded. Templates you want to print several times use plain include.
Can an included PHP file return a value?
Yes. A return at the top level of the included file ends it and becomes the value of the include or require expression: $config = require 'config.php'; with return ['debug' => true]; in config.php. Without a return, include evaluates to 1.
Does an included file have access to my variables?
Yes. The included code runs in the scope of the line that includes it: at the top of a script it sees the global variables, and inside a function it sees that function's local variables and nothing else. Variables it creates land in the same scope.
Should I write include('file.php') or include 'file.php'?
Both work, because include and require are language constructs, not functions; the parentheses just wrap the path. Most style guides write require 'file.php'; without them.