include_once in php – PHP include and include_once

In PHP, we use include and include_once to include files. Because when we work on a project, we need the same data in many places, for that we do not write the same data in each file and include it by writing it in another file.

Like – Header and Footer remain the same in most websites, or there are many such functionality, which we need to write again and again, so the best way to use such content is that you can put them in another file. Write include.

PHP include and include_once Syntax :

[php]
include(‘path-to-file’);
include_once(‘path-to-file’);
[/php]

PHP Include Example :

file1.php
[php]

[/php]

include.php
[php]

[/php]

Why we use include or include_once ?

Code Re-usability

include or include_once is the biggest code re-usability, as I have already told that we do not need to write the same content again and again, for this PHP has given features to include files.

Easy To Update

Since now the same code used in many places is in one place, so in future if we need to update that code, then simply we can update the code from one place, and where – where that file is included.

Change will come automatically everywhere.

Difference Between include And include_once

The biggest difference between include and include_once is that when we use include, it includes the file every time even if the same file is already included, due to which PHP generates a fatal error.

When a file is included two or more times.

File : first.php
[php]

[/php]

File : second.php
[php]

[/php]

[php]
Fatal error: Cannot redeclare getData() (previously declared in C:\xampp\htdocs\getData\first.php:4) in C:\xampp\htdocs\getData\first.php on line 5

[/php]

So in the example you can see how a fatal error was generated by repeatedly including the same file.

This is because variables in PHP can be re-declared any number of times (except for constant variables) but not functions, and PHP generates Fatal Error because of the same file being included repeatedly because of the functions. To avoid this problem, we use include_once().

Including files using include_once will not generate any error even if the same file is included. Because include_once includes the file only if the same file has not been included before.

File : second.php
[php]

[/php]

I hope, now you must have understood the proper difference between include and include_once.

Best Practice With include and include_once :

If we include a file inside a function by using include or include_once, then the scope of the code (variables etc.) defined in that file will be in that function itself, the variables defined in that file we will define in the function. Will not be able to access outside.

And if you want to access the variables defined in that file, then the defined variables have to be made global.

File : first.php
[php]

[/php]

File : second.php
[php]

[/php]

[php]
value inside function of x : 10
Notice: Undefined variable: x in C:\xampp\htdocs\getData\second.php on line 10
value out of the function
[/php]

As you can see, first.php file is included in getData() function in which $x variable is defined, and when access this variable inside the function we get its value but when it is out of When accessing the function, PHP generates a warning.

Now, if you want to access variables defined in a file included inside a function outside that function, then those variables have to be made global before the file is included.

File : second.php
[php]
“;
}

/*call the function and print variable*/
getData();
echo “value out of the function : $x”;
?>
[/php]

[php]
value inside function of x : 10
value out of the function : 10
[/php]

Leave a Comment