Explanation of PHP Include File Example

  • 2021-11-24 01:05:56
  • OfStack

Server-side contains (SSI) for creating functions, headers, footers, or elements that can be reused on multiple pages.

The include (or require) statement takes all text/code/markup that exists in the specified file and copies it to the file using the include statement.

Inclusion files are useful if you need to refer to the same PHP, HTML, or text on multiple pages of your Web site.

PHP include and require Statements

With the include or require statement, you can insert the contents of an PHP file into another PHP file (before the server executes it).

The include and require statements are the same except for error handling:

require generates a fatal error (E_COMPILE_ERROR) and stops the script include generates only warnings (E_WARNING) and the script continues

Therefore, use include if you want to continue execution and output the results to the user, even if the include file is missing. Otherwise, in framework, CMS, or complex PHP application programming, always use require to reference critical files to the execution stream. This helps improve the security and integrity of your application in the event that a critical file is accidentally lost.

Including files saves a lot of work. This means that you can create standard headers, footers, or menu files for all pages. Then, when the page header needs to be updated, you only need to update the page header include file.

Grammar


include 'filename';

Or


require 'filename';

PHP include Example

Example 1

Suppose we have a standard footer file named "footer. php", like this:


<?php
echo "<p>Copyright © 2006-" . date("Y") . " W3School.com.cn</p>";
?>

To reference this footer file in 1 page, use the include statement:


<html>
<body>

<h1> Welcome to our home page! </h1>
<p>1 Paragraph text. </p>
<p>1 Paragraph text. </p>
<?php include 'footer.php';?>

</body>
</html>

Example 2

Suppose we have a standard menu file named "menu. php":


<?php
echo '<a href="/index.asp" rel="external nofollow" > Home page </a> -
<a href="/html/index.asp" rel="external nofollow" >HTML  Tutorials </a> -
<a href="/css/index.asp" rel="external nofollow" >CSS  Tutorials </a> -
<a href="/js/index.asp" rel="external nofollow" >JavaScript  Tutorials </a> -
<a href="/php/index.asp" rel="external nofollow" >PHP  Tutorials </a>';
?>

This menu file is used by all pages in the Web site. The specific method is (we used 1 < div > Element, so that you can easily set styles through CSS in the future):


<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

Suppose we have a file named "vars. php" with 1 variables defined:


<?php
$color=' Silver-coloured ';
$car=' Mercedes-Benz sedan ';
?>

Then, if we reference the "vars. php" file, we can use these variables in the call file:


<html>
<body>

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

</body>
</html>


Related articles: