PHP Basic Tutorial
MySQL Connection
PHP Advanced
The include
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.
include 'filename.php';
include
statement is executed, PHP will insert the contents of the specified file at the point where the include
statement appears in the script.filename.php
can be a relative or absolute path.Assume we have a standard footer file called "index.php", that looks like this:
Try it yourself
Assume we have a standard footer file called "header.php", that looks like this:
Assume we have a standard footer file called "footer.php", that looks like this:
In this example:
header.php
file is included at the top of index.php
to display the header.footer.php
file is included at the bottom of index.php
to display the footer.include
and require
:include
: Will generate a warning (E_WARNING
) if the file is not found but the script will continue executing.require
: Will generate a fatal error (E_COMPILE_ERROR
) and stop the script execution if the file is not found.Try it yourself
If we do the same example using the require
statement, the echo statement will not be executed because the script execution dies after the require
statement returned a fatal error:
Try it yourself
Note
Use require
when the file is required by the application.
Use include
when the file is not required and application should continue when file is not found.