PHP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Include Require

PHP Include Require

shape Description

The functions include() and require() always plays a great role in PHP. Suppose to repeat module more than one once in a program, there is no need to rewrite code each and every time. Just include the module using include() module. Later stages, to modify anything in that module, modify only in one location.Then automatically entire project gets updated. require() function is also used to include the file in php.

Difference between Include and Require

shape Description

Require function is also used to include the file into code, like include function. Both will perform smaller functionalities. But require() function is used when the file is mandatory. Include(): If any file doesn't exist in the file structure, which is included in the code by using include function, then the control will go to the next statement and the execution will continue. It won't stop the controller.
include 'filename';
Require(): If the file doesn't exist in the file structure, which is included in the code by using require function, then the control will terminate the flow of execution and gives an error. So when a file is mandatory only require function has to be used and properly checked, whether the file exist in the file structure of not.
require 'filename';

shape Examples

menu.php [php] <?php echo '<a href="/default.asp">Home</a> - <a href="http://www.splessons.com/lesson/html-introduction/">HTML Tutorial</a> - <a href="http://www.splessons.com/lesson/bootstrap-3-tutorial/">Bootstrap Tutorial</a> - <a href="http://www.splessons.com/lesson/javascript-tutorial/">JavaScript Tutorial</a> - <a href="http://www.splessons.com/lesson/php-tutorial/">PHP Tutorial</a>'; ?> [/php] include() : abc.php [php] <html> <body> <div class="menu"> <?php include 'menu.php';?> </div> <h1>Welcome to SPLessons page!!</h1> </body> </html> [/php] Example: require() [php] <html> <body> <div class="menu"> <?php require 'menu.php';?> </div> echo "Welcome to SPLessons page!!"; </body> </html> [/php] When above code is executed, when any error occurs, the echo statement will not be executed.

Summary

shape Key Points

The chapter PHP Include Require draws out few main points:
  • require() function is used when the file is mandatory.
  • include() function cannot interrupt the flow of execution.