PHP Include File

PHP Include File

ᴰᵒᵖᵖᵉˡᵍᵃⁿᵍᵉʳ 多佩尔甘格尔

PHP allows you to include file so that a page content can be reused many times. There are two ways to include file in PHP.

  1. include
  2. require

 

Advantage

Code Reusability: By the help of include and require construct, we can reuse HTML code or PHP script in many PHP scripts.

 

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

File: menu.html

<a href="http://www.skyapper.com">Home</a> |   
<a href="http://www.skyapper.com/php-tutorial">PHP</a> |   
<a href="http://www.skyapper.com/java-tutorial">Java</a> |    
<a href="http://www.skyapper.com/html-tutorial">HTML</a>  

 

File: include1.php

<?php include("menu.html"); ?>  
<h1>This is Main Page</h1>  

 

Output:

Home | PHP | Java | HTML

This is Main Page

 

PHP require example

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

File: menu.html

<a href="http://www.skyapper.com">Home</a> |   
<a href="http://www.skyapper.com/php-tutorial">PHP</a> |   
<a href="http://www.skyapper.com/java-tutorial">Java</a> |    
<a href="http://www.skyapper.com/html-tutorial">HTML</a>  

 

File: require1.php

<?php require("menu.html"); ?>  
<h1>This is Main Page</h1>  

 

Output:

Home | PHP | Java | HTML

This is Main Page

 

PHP include vs PHP require

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.

 


Report Page