Friday, October 6, 2017

PHP Include File


What is PHP Include File ?

PHP allows you to include another file so that a page content can be reused many times. There are two ways to include file in PHP :
1. Using Include construct.
2. Using Require construct.

Why do we need PHP Include File ?

Most important purpose to include php file is code reusability. By the help of include and require construct, we can reuse HTML code or PHP script in many PHP scripts.

PHP include File example.

PHP include is used to include file on the basis of given path. You may use relative or absolute path of the file. Let's see a simple PHP include example.

Header.html
<a href="http://www.skptricks.com/">Home page</a> |   
<a href="http://www.skptricks.com/p/php.html">PHP</a> |   
<a href="http://www.skptricks.com/search/label/java">Java</a> |    
<a href="http://www.skptricks.com/search/label/html5">HTML</a>y

Include_file.php
<?php include("Header.html"); ?>  
<h1>This is skptricks index page</h1>

Output:
-----------------------------
Home | PHP | Java | HTML
This is skptricks index page

PHP Require example.

PHP require is similar to include. Let's see a simple PHP require example.

Header.html
<a href="http://www.skptricks.com/">Home page</a> |   
<a href="http://www.skptricks.com/p/php.html">PHP</a> |   
<a href="http://www.skptricks.com/search/label/java">Java</a> |    
<a href="http://www.skptricks.com/search/label/html5">HTML</a>y

Include_file.php
<?php require("Header.html"); ?>  
<h1>This is skptricks index page</h1>

Output:
-----------------------------
Home | PHP | Java | HTML
This is skptricks index page

Note : Using above way we can use php include html file.

PHP include file vs PHP require file

If file is missing or inclusion fails, include allows the script to continue but require halts the script producing a fatal E_COMPILE_ERROR level error.


No comments:

Post a Comment