YogeshChauhan.com
include, include_once, require, require_once in PHP
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.

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

WordPress: How to loop through a repeater field in ACF?WordPressHow to Use SQL MAX() Function with Dates?SQL/MySQLCrypto.getRandomValues() method in JavaScriptJavaScriptCurrencyPipe in Angular 9 with ExamplesAngularKeyValuePipe in Angular 9AngularHow to select an element using its ID without the high specificity of the ID selector?CSS