PHP - Include & Require

Include

The include statement includes and evaluates the specified file.the following example of include we try to make two file one file content user information and second is display user data using include.

Example of Include

info.php


<?php

$fname = 'Bharat';
$lname = 'Makwana';

?>
							

display.php


<?php

include 'info.php';
echo "Welcome Mr"." ".$fname." ".$lname;
 
?>
							

Output



Welcome Mr. Bharat Makwana							
							

Require

Require is a same work as includes and evaluates the specified file.but require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.


<?php

require 'info.php';
echo "Welcome Mr"." ".$fname." ".$lname;
 
?>
							

The above example info.php are not available here but used require this script stop execution of this script.

Difference Between Include and Require

Sr.No

Include

Require

1

include will attempt to load the specified file, but will allow the script to continue if not successfully loaded.

require on the other hand will cause a "Fatal Error" to occur if the specified file is not successfully loaded.

2

include produces a Warning if the file cannot be loaded. (Execution continues)

require will throw a PHP Fatal Error if the file cannot be loaded. (Execution stops)

Share Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit