Header menu

_________________________________________________________________________________

Wednesday 15 January 2014

Explain difference between include , require and require_once in php ?


To include the content of a PHP file into another PHP file before the server executes it. There are three PHP functions which can be used to include one PHP file into another PHP file.
  • The include() Function 
  • The require() Function
  • The require_once() Function
These functions helps in creating functions, headers, footers, or elements that can be reused on multiple pages. This help developers to change the layout of complete website with minimal effort. If there is any change needed then instead of making change in large numbers of files just change included file.

The include() Function

The include() function copies all text from file which is passed as parameter into the file that uses the include function. If there is any problem in loading thefile then the include() function generates a warning but the script will continue execution.

Usuage of include function :
Below is code of my main file .
<?php include("one.php"); ?>

Here the code of one.php will be copied to main file.

The require() Function

The require() function copies all text from file which is passed as parameter into the file that uses the include function. If there is any problem in loading the file then the require() function generates a fatal error and halt the execution of the script.
Usuage of require function :
Below is code of my main file .
<?php include("two.php"); ?>

Here the code of two.php will be copied to main file.



Difference between include() and require()
The main difference in require() and include() is the way of handling error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if there is some issue in file loading.



The require_once() Function

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



No comments:

Post a Comment