SHARE

Difference between require and include statement

In PHP , require statement and include statement are buit in functions . These functions are used to manage or add external file in your present php file. Both are used to include files ,but still there are some differences in these statments .

require statment

require statement
require statement

When require statement is used , then it means this file is compulsory for complete page loading . It is strict statement . Suppose you create a index.php using following source code . After that a header.php is created and include this file in index.php using require statement .

After that run index.php on web browser .  In result header.php content is displayed on top of the web page header .And paragraph text displayed in next .

Now try something else . Create a php coding error in index.php ,such as change file name header.php to head.php in php code . save this file and run again .

There is an error in file name so header.php not load correctly . In the result a fatal error comes and all other are terminated .

Only error log is displayed on web page . require statement tells the browser to terminate rendering if header is not load correctly . Complete web page is blocked .

include statement

include statement
include statement

When include statement is used to add header.php .It means we tell the browser to include header also on this web page ,but if header.php is not load correctly and something is wrong ,then skip this loading and process to other code .

Only error warning is displayed and other scripts are still executed .

Example

Create two files again ,index.php and header.php . This time use include statement to add header.php file . Change file name header.php to head.php in php code to make as a coding mistake . Save this file and run index.php on web browser .

This time result is different . After coding mistake header.php is not load correctly . This step is terminated and an error warning is displayed but next code is rendered .

As result  a paragraph is displayed with text “This is a paragraph” and <h1> is also displayed .

index.php source code with require statement

<html>
<head>
</head>
<body>
<?php require 'header.php'  ; ?>
<p> This is a paragraph </p>
</body>
</html>

index.php source code with include statement

<html>
<head>
</head>
<body>
<?php include 'header.php'  ; ?>
<p> This is a paragraph </p>
</body>
</html>

header.php source code

<?php
echo "<h1>Tectrick</h1></br>"
?>

After using the upper code hope you can understand working of require and include statement . require statement is used for very secure web application functions such as banking sites logins . include statement is used for general purpose file adding such as adding header ,sidebar or footer in php web page .

 

SHARE
Previous articleHow To On PHP Error Reporting To Find Php Errors
Next articleDefinition of built in function in programming
Manu Mahajan Born 26 Dec 1980 ,Is a founder of Tectrick and MM Toy World. www.tectrick.org is a free digital helping guide on many usable topics to the readers in various method such as Youtube videos , Articles and social media conversation MM Toy World is manufacturer , wholesaler and online retailer of soft toys, personalized gifts ,toys corporate gifts .