The include file is useful when you want to copy all the content from another php file. The included code copies all the content available in that file and presents it in the current file. It is very useful when you want to copy certain php code, HTML code or text file on multiple pages.

Example:

footer.php file has following code

[php]<?php
echo "Copyright © 1999-2018 W3Schools.com";
?>[/php]

Our include file:

[php]<?php include ‘footer.php’;?>[/php]

Code explanation

Assume you have common footer section. In this case, it is better to update the footer in a file which reflects the result on all another file of the website where include statement is added. This saves lots of time updating each page manually.

By adding above include file in the footer section you can fetch the copyright text from the footer.php file.

Example 2:

vars.php file

[php]<?php
$color=’red’;
$car=’BMW’;
?>[/php]

Our incude file:

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

In the above code, we will get the $color and $car variable value from the include file vars.php by stating the include statement in the HTML code.

Related Posts

Leave a Reply