PHP Include and Require

PHP Include and Require function or statement are used to include PHP, HTML, or text on multiple pages of a website. One file can use code of another file using PHP Include and Require function.

PHP Include and Require allows us to include file so that a page content can be reused many times. It is very helpful to include files when you want to apply the same HTML or PHP code to multiple pages of a website.

There are two ways to include file in PHP.

1. include()
2. require()

Both include and require are same to each other. Difference between include() and require() as per as below.

include() - It generates a warning E_WARNING, and continue the execution of the script when file is not found.
require() - It generates a fatal error E_COMPILE_ERROR and execution of the script will be stopped when file is not found.

Advantages :
1. Code Reusability - Same code or functionality can be used in many files.
2. Easy to update code - If you want to change any common thing in website pages, then you can update it using included file.

PHP Include

PHP Syntax:

include 'filename ';
Or   
include ('filename');

PHP Example :

File : navigation.html
<a href="https://www.aryatechno.com/blog.html">Home</a> | <a href="https://www.aryatechno.com/category/1/php.html">PHP</a> | <a href="https://www.aryatechno.com/category/2/mysql.html">MYSQL</a> | <a href="https://www.aryatechno.com/category/5/wordpress.html">Wordpress</a> 

File : home.php
<?php
   include("navigation.html");
?> 
<h1>This is Home Page</h1> 

Output:

 

This is Home Page

Home | PHP | MYSQL | Wordpress

 

PHP Require

PHP Syntax:

require 'filename';
Or
require('filename');

PHP Example :
File : navigation.html
<a href="https://www.aryatechno.com/blog.html">Home</a> | <a href="https://www.aryatechno.com/category/1/php.html">PHP</a> | <a href="https://www.aryatechno.com/category/2/mysql.html">MYSQL</a> | <a href="https://www.aryatechno.com/category/5/wordpress.html">Wordpress</a> 

File : home.php
<?php
   require("navigation.html");
?> 
<h1>This is Home Page</h1> 
 

Output:

This is Home Page

Home | PHP | MYSQL | Wordpress

 

PHP Include_once

The include_once function or statement is used to embed PHP code from another file. The file will be included just once if file is included multiple times . It generates a warning E_WARNING, and continue the execution of the script when file is not found like include function or statement. if the code from a file has already been included, it will not be included again.

PHP Syntax:

include_once 'filename ';
Or   
include_once ('filename');

PHP Example :

Output:

PHP Require_once

The require_once function or statement is used to embed PHP code from another file. The file will be included just once if file is included multiple times . The require_once generates a fatal error E_COMPILE_ERROR and execution of the script will be stopped when file is not found like require function or statement. if the code from a file has already been included, it will not be included again

PHP Syntax:

require_once 'filename';
Or
require_once('filename');

PHP Example :

Output:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

64118