You can insert the content of one PHP file into another before the server executes it using the include
or require
statement.
The include
and require
statements function similarly, but differ in their handling of failures:
require
will trigger a fatal error (E_COMPILE_ERROR) and halt the script if the file is not found.include
will produce a warning (E_WARNING) but continue executing the script if the file is missing.For critical files where the execution should not continue if the file is missing, use require
. For less critical files where you want to continue execution even if the file is absent, use include
. This is particularly important for frameworks, CMSs, or complex PHP applications to maintain security and functionality.
Including files can streamline development by allowing you to create a standard header, footer, or menu that is used across all web pages. When an update is needed, you only need to modify the included file rather than every individual page.
include ‘filename‘; or require ‘filename‘; |
Assume we have a standard footer file named “footer.php” that appears as follows:
<?php echo “<p>Copyright © 1999-” . date(“Y”) . ” W3Schools.com</p>”; ?> |
To add the footer file to a page, use the include
statement:
<html> <body> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> <?php include ‘footer.php’;?> </body> </html> |
Assume we have a standard menu file named “menu.php”:
<?php echo ‘<a href=”/default.asp”>Home</a> – <a href=”/html/default.asp”>HTML Tutorial</a> – <a href=”/css/default.asp”>CSS Tutorial</a> – <a href=”/js/default.asp”>JavaScript Tutorial</a> – <a href=”default.asp”>PHP Tutorial</a>’; ?> |
All pages on the website should utilize this menu file. Here’s how it can be implemented (using a <div>
element to facilitate styling with CSS later):
<html> <body> <div class=”menu”> <?php include ‘menu.php’;?> </div> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> </body> </html> |
Assume we have a file named “vars.php” that contains some defined variables:
<?php $color=’red’; $car=’BMW’; ?> |
When we include the “vars.php” file, the variables defined in it become accessible in the including file.
<html> <body> <h1>Welcome to my home page!</h1> <?php include ‘vars.php’; echo “I have a $color $car.”; ?> </body> </html> |
The require
statement is also used to include a file in PHP code.
However, there is a significant difference between include
and require
: if PHP cannot find a file included with include
, the script will continue to execute.
<html> <body> <h1>Welcome to my home page!</h1> <?php include ‘noFileExists.php’; echo “I have a $color $car.”; ?> </body> </html> |
If we use the require
statement for the same example, the echo
statement will not be executed because the script execution halts after require
returns a fatal error.
<html> <body> <h1>Welcome to my home page!</h1> <?php require ‘noFileExists.php’; echo “I have a $color $car.”; ?> </body> </html> |
Use Use |