Difference between include() and require() function in PHP 7

Basically, Difference between include() and require() function in PHP 7 is only one has the fatal error and another one has warning error. but the more basic difference is given below:

<?php
//
include("worker.php");

?>

 

<?php

require("worker.php");

?>

 

First up, neitherinclude requirefunctions, they are constructs. It is therefore not necessary to call them using parentheses like;include('file.php') instead, it is prefered to use.include 'file.php'

The difference betweeninclude andrequire arises when the file being included cannot be found: include will emit a warning (E_WARNING) and the script will continue, whereas willrequire emit a fatal error (E_COMPILE_ERROR) and halt the script. If the file being included is critical to the rest of the script running correctly then you need to use.require

You should pick up on any fatal errors thrown by duringrequire the development process and be able to resolve them before releasing your script into the wild; however, you may want to consider using toinclude put in place a plan B if it’s not that straightforward:-

<?php
if (@include 'file.php') {
	// Plan A
} else {
	// Plan B - for when 'file.php' cannot be included
}

In this example include is used to grab ‘file.php’, but if this fails we suppress the warning using and@ execute some alternative code. include will return false if the file cannot be found.

include_once and behaverequire_once like andinclude requirerespect, except they will only include the file if it has not already been included. Otherwise, they throw the same sort of errors.


Difference between include() and require() function in PHP 7

to summarize throwsinclude a warning and throwsrequire a fatal error and ends the script when a file cannot be found. Simple as that!

 

Learn More About PHP Functions ( Include and required ) 

Loading

Leave a reply


This site uses Akismet to reduce spam. Learn how your comment data is processed.