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 :

 include('path-to-file');
 include_once('path-to-file');

PHP Include Example :

file1.php

<?php  
  $x = 'x defined in file1';            
?>

include.php

<?php  
  include('file1.php');  
  echo $x;          
?>

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.

See also :  how to send attachment in mail in php?

When a file is included two or more times.

File : first.php

<?php  
  function getData()
  {
  	echo "function inside first.php";
  }           
?>

File : second.php

<?php  
  include('first.php');  
  /*again include it so that we can differentiate */
  include('first.php');  
  /*call the function*/ 
  getData();          
?>

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

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  
  include_once('first.php');  
  /*again include */
  include_once('first.php');  
  /*call the function*/ 
  getData();          
?>

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  
  $x = 10;             
?>

File : second.php

<?php  
   function getData()
  {
  	include_once('first.php');
  	echo "value inside function of x : ".$x;
  }

  /*call the function and  print both variables*/ 
  getData();
  echo "value out of the function : $x 
";       
?>

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 

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.

See also :  Advantages of php - Top 10 Fundamental PHP Advantages And Disadvantages

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  
  function getData()
  {
  	global $x;
  	include_once('first.php');
  	echo "value inside function of x : $x <br>";
  }

  /*call the function and  print variable*/ 
  getData();
  echo "value out of the function : $x";       
?>

value inside function of x : 10
value out of the function : 10

Leave a Reply

Your email address will not be published.