Theinclude statement in PHP is used to include the content of one PHP file into another PHP file. It allows you to reuse code across multiple pages, making your applications more modular and easier to maintain.
The include(or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.
Syntax:
include 'filename.php';
How it Works:
When the includestatement is executed, PHP will insert the contents of the specified file at the point where the includestatement appears in the script.
The filename.php can be a relative or absolute path.
PHP include Examples
Assume we have a standard footer file called "index.php", that looks like this:
If we do the same example using the requirestatement, the echo statement will not be executed because the script execution dies after the requirestatement returned a fatal error:
Example
<?php
echo"Attempting to require a missing file...<br>";
// File does not exist
require'missing_file.php';
echo"This message will not be displayed if the file is missing.<br>";
We use cookies and similar technologies to enhance your browsing experience, serve personalized content and advertisements, and analyze our traffic.
By clicking "Accept All", you consent to our use of cookies and the processing of your personal data for these purposes.
You can manage your preferences or learn more in our
Privacy Policy and
Cookie Policy.