Include PHP files: Multiple ways

As a developer, we mostly know to include n number of PHP files using two functions. First, ‘include()’ or ‘include_once()’ and the other ‘require()’ or ‘require_once()’. Both has their own way of including files and executing the code within them. The only confusion we sometimes have is when to use what. Well, the answer is like:

Clarification No.1

The ‘require()’ function is similar to ‘include()’; except the the way it handles errors differently. When an error occurs, the ‘include()’ function generates a warning and the script continues its execution. Whereas the ‘require()’ throws a fatal error and will stop the code execution.

Clarification No.2

The ‘require_once()’ statement is identical to ‘require()’ except PHP will check if the file has already been included, and if yes, not include (require) it again.

Coding with a modular approach is the greatest way to work and that helps us while a look-back of the code is essential.

Approach 1






Approach 2

For ref:
http://php.net/manual/en/function.include.php
http://www.w3schools.com/php/php_includes.asp
http://php.net/manual/en/function.require-once.php

Share

Post Author: Shreyo

is a developer & involved in project management; loves to learn, guide and keep himself updated on UI/UX and WordPress. He is also an amateur photographer mostly macro shots.