Difference between require and include in PHP

You might have just started writing PHP code or you might be preparing for an interview in one of your dream companies. This is one of the basic doubts which a large number of developers have today. In this post, I will be comparing and explain you the difference between require and include in PHP in a very basic manner.

Difference between require and include

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

To demonstrate it let’s create the following fileL

//index.php
<?php
	error_reporting(E_ALL);
	ini_set('display_errors',1);
	include('file1.php');
	echo 'Concatly include not found but script running';
?>

In the above script, we are trying to include a file which does not exist.

Output:
Warning: include(file1.php): failed to open stream: No such file or directory in /var/www/html/test/index.php on line 4

Warning: include(): Failed opening 'file1.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/test/index.php on line 4
Concatly include not found but script running

In the above example, file1.php is not found as we have not created the file in the provided location. As we have enabled error_reporting, a warning is generated that file1.php is not found but the script continues to run without throwing any fatal error. Now let’s modify the script a bit as follows:

//index.php
<?php
	error_reporting(E_ALL);
	ini_set('display_errors',1);
	include('file1.php');
	echo 'Concatly include not found but script running';

	require('file2.php');
	echo 'Require working when file is missing';
?>

In the above script, after trying to include file1.php, we are trying to require file2.php which also does not exist.


Warning: include(file1.php): failed to open stream: No such file or directory in /var/www/html/test/index.php on line 4

Warning: include(): Failed opening 'file1.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/test/index.php on line 4
Concatly include not found but script running
Warning: require(file2.php): failed to open stream: No such file or directory in /var/www/html/test/index.php on line 7

Fatal error: require(): Failed opening required 'file2.php' (include_path='.:/usr/share/php') in /var/www/html/test/index.php on line 7

As shown in the output above, the echo statement after include is appearing, however the second statement does not appear in the output and the script ends in between with the fatal error.

Conclusion

I personally prefer to use require in all my scripts as a missing file will definitely break some functionality in my script. You might be using a core functionality or calling a very important function declared in that precious file which is missing. So, it is considered best to always use require for all your files. In a certain case where the file is optional and the script should continue to run, you may use include.

You may read about include and require in detail on the official website.

http://php.net/manual/en/function.include.php

http://php.net/manual/en/function.require.php

Do leave your valuable comments so that I can improve on my articles. If you have any doubts in the difference between require and include, feel free to comment in this section.

Spread the Knowledge
Categories:

Leave a Reply

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