Yogesh Chauhan's Blog

include, include_once, require, require_once in PHP

in PHP on January 21, 2020

It's really easy to include files in PHP and when we progress in PHP, we try to keep our code clean and in separate files and execute it by including whenever necessary. We might keep a library of functions a=in a separate file and include it in the final script whenever required. We do that to reduce writing down same code again and again. We might use other programmers' code just by including their files as well. In all those scenarios, we need include or require statements to include files.

The include Statement

Using include we tell PHP to include the contents of a particular file and execute it if there is any code to execute. All it does is to include the contents of the file we are including and makes it a bigger script when it compiles the code. 

For example:


//code inside PHP

include "functions.php";
...
...
other code
...

include_once

This is really helpful statement as there is a bit of problem with include and this one solves it.

When we write include statement, PHP included the whole file. Let's understand by example:


//code inside PHP

include "functions.php";
...
...
other code
...

include "math.php";

//end

Let's assume we have functions in math.php as well. For example the content of the math.php:


//code inside PHP

include "functions.php";
...
more code
..
//end

Now we are including functions.php and math.php just once in some script. But math.php also has functions.php inside it. So, the script is going to include the functions.php one more time which is redundant code. It's already been included once.

To skip including files again and again we can use include_once and PHP will include that file just for one time and won't keep including it again and again.

PHP will ignore the code if it's included already if we have used included_once. To do that PHP compares the absolute file paths after all relative paths are resolved.

require and require_once

This statement encounters one more potential problem that include and include_once have.

If we use include or include_once, PHP will try to attempt the file and if it can't access it, it won't bother. It will keep executing the remaining code and will show the output. So, basically PHP will run the script even if the file is not found.

But what if we absolutely need that file or there won't be a meaningful output?

That's why we need require statement. When the file is required no matter what, use require to include the file. For example:


//code inside PHP 

require "functions.php";

require_once "math.php";

//end

If PHP can't find the required file then it will give a fatal error. In the case of include, it won't give you a fatal error.


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to compress images with gulp in WordPress?WordPressHow to progressively load images and add a blurry placeholder?CSSWhat is the difference between let and var in Swift?SwiftFile System Integrity: How to Keep an Eye on Your Files and Folder Change?MiscellaneousIN Operator in PostgreSQLPostgresHow to Use the EXISTS and NOT EXISTS Operator with a Subquery in SQL and MySQL?SQL/MySQL