Difference Between Include And Require In PHP

Functional Work :

Both functions perform similar work. Both functions will include and evaluates the specific file while executing the code.

Functional Difference :

if include() is not able to find a specified file on location at that time it will throw a warning however, it will not stop script execution. For the same scenerio require() will throw fatal error and it will stop the script execution.

PHP include and require Statements

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

The include and require statements are identical, except upon failure:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue

So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application’s security and integrity, just in-case one key file is accidentally missing.

Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

Syntax

include ‘filename‘;

or

require ‘filename‘;

include Examples

Example 1

Assume we have  footer file called “footer.php”, that looks like this:

<?php

echo “<p>Copyright &copy; 2017-” . date(“Y”) . ” www.webtechsource.com</p>”;

?>

To include the footer file in a page, use the include statement:

Example

<html>

<body>

<h1>Welcome to webtechsource page!</h1>

<p>webtechsource is design and development company in jaipur</p>

<?php include ‘footer.php’;?>

</body>

</html>

 

PHP include vs. require

The require statement is also used to include a file into the PHP code.

However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:

Example

<html>

<body>

<h1>Welcome to Webtech source page!</h1>

<?php include ‘noFileExists.php’;

echo “I have a $color $car.”;

?>

</body>

</html>

If we do the same example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:

Example

<html>

<body>

<h1>Welcome to webtechsource page!</h1>

<?php require ‘noFileExists.php’;

echo “written by dayanand saini”;

?>

</body>

</html>

Use require when the file is required by the application.

Use include when the file is not required and application should continue when file is not found.

I hope above all solutions worked for you, share this post  and enjoy your day.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

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

33 − = 30

%d bloggers like this: