PHP Include Files


The merge statement (or need to) take all the text / code / quotes present in the specified file and copy it to a file using the merge statement.

Uploading files is very helpful if you want to include the same PHP, HTML, or text on multiple web pages.


PHP include and require Statements

It is possible to insert the contents of one PHP file into another PHP file (before the server does it), with a compilation or need statement.

Including statements and requirements are the same, with the exception of failures:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the scrip
  • include will only produce a warning (E_WARNING) and the script will continue

Therefore, if you want the execution to continue and show users the output, even if the inserted file is missing, use the compilation statement. If not, in the case of FrameWork, CMS, or complex PHP application code, always use the required statement to insert the key file into the workflow. This will help you to avoid compromising the security and integrity of your application, in the event that a single key file is accidentally lost.

Uploading files saves a lot of work. This means you can create a custom header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header to include the file.

Syntax
include 'filename';

or

require 'filename';

PHP include Examples

Example 1

Suppose we have a standard foot file called "footer.php", which looks like this:

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

To add a footer file to the page, use a compilation 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

Suppose we have a standard menu file called "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 site should use this menu file. Here's how it can be done (we use the

feature to make the menu easier to write 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

Suppose we have a file called "vars.php", which has some defined features:

<?php
$color='red';
$car='BMW';
?>

Then, when we add the file "vars.php", the variable can be applied to the 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 insert the file into the PHP code.

However, there is one major difference between integration and demand; if the file is include with the installed statement and PHP cannot find it, the script will continue to perform:


Example
<html>
<body>

<h1>Welcome to my home page!</h1>
<?php include 'noFileExists.php';
echo "I have a $color $car.";
?>


</body>
</html>

If we make the same example using the require statement, the echo statement will not be used because script writing dies after the require statement returned a bad error:


Example
<html>
<body>

<h1>Welcome to my home page!</h1>
<?php require 'noFileExists.php';
echo "I have a $color $car.";
?>


</body>
</html>

Use is require when the file is required by the application.

Usage include when the file is not required and the application should continue when the file is not available.