Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Include

PHP include and require Statements

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.

Syntax

include ‘filename‘;

or

require ‘filename‘;

PHP include Examples

Example 1

Assume we have a standard footer file named “footer.php” that appears as follows:

<?php
echo “<p>Copyright &copy; 1999-” . date(“Y”) . ” W3Schools.com</p>”;
?>

To add the footer file to a page, use the include statement:

Example

<html>
<body>

<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include ‘footer.php’;?>

</body>
</html>

Example 2

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):

Example

<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>

Example 3

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.

Example

<html>
<body>

<h1>Welcome to my home page!</h1>
<?php include ‘vars.php’;
echo “I have a $color $car.”;
?>


</body>
</html>

PHP include vs. require

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.

Example

<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.

Example

<html>
<body>

<h1>Welcome to my home page!</h1>
<?php require ‘noFileExists.php’;
echo “I have a $color $car.”;
?>


</body>
</html>

Use require when the file is essential for the application.

Use include when the file is not crucial, and you want the application to continue running even if the file is missing.