PHP Include and Require statements

Include

In PHP, include statement allows us to include or evaluate PHP file into another PHP file. It includes all the text/code/markup of the specific file and copies into a source file.

Use of Include in PHP

  • When you want the same text/code/markup across multiple files on your website you can create a separate file and use it in multiple files through an include statement.
  • If the include statement, the source file is missing it will emit the warning and continue to execute the output.

Syntax

<? php
	include ‘file_name’;
?>
  • Using the include statement saves a lot of work.
  • You can create a header, footer, or menu file and include it in your PHP file. Then, when you want to change either header or footer or menu you have to manipulate that particular file and not the entire PHP file.
  • PHP provides ‘include_once’ statement which can be used to include the file only once. It is used to prevent the situation where you have included the same file more than one time in the script.

Example:-

File fruit.php

<?php
	$fruit = "Apple";
	$color = "red";
?>

File main.php

<?php
	include 'fruit.php';
	echo "The " .$fruit." is " .$color. " in color "; 
?>

Output:

Output:-

 The  Apple  is  red  in color

Require

In PHP, the require statement is the same as to include statement. The only difference is that it will produce the compilation error upon failure while the include statement only generates the warning.

Use of Require in PHP

  • It is used in the application where it is necessary to include files. If it is missing it will stop the execution.
  • It maintains the security and integrity of the application.
  • The require statement produces a fatal error (E_COMPILE_ERROR) and stops the script.

Syntax:

<?php
	require 'file_name';
?>

Example:-

File fruit.php

<?php
	$fruit = "Apple";
	$color = "red";
?>

File main.php

<?php
	require 'fruit.php';
	echo "The " .$fruit." is " .$color. " in color "; 
?>

Output:-

 The  Apple  is  red  in color

Example 2:

<?php
	reuire ‘filedoesnotexist.php’;
	echo "The " .$fruit." is " .$color. " in color "; 
?>

Output:

 Fatal error: main(): Failed opening required ‘filedoesnotexist.php’

include_once and require_once

Sometimes you will include the file in main file more than once and it will conflicts with same file and it will throw a error. So for this PHP has provided two more functions which includes files only once no matter how many times you have used include_once or require_once.

Example:

<?php
	include_once 'fruit.php';
	echo "The " .$fruit." is " .$color. " in color ";
	require_once 'fruit.php';
	echo "The " .$fruit." is " .$color. " in color ";
?>

As in the above example, I have added our previous file fruit.php twice with include_once and require_once. it will not give us any error as it will add the file once. Also, I recommend you to use include_once and require_once so these types of errors can be omitted.

Output:

The  Apple  is  red  in color