Rate Us :

Share with Friends :


PHP-Include & Require :

include :

By using this concept we can include a PHP script in another script. If we include an external script in the current PHP script we can access functions, classes, variables and constants of external script from current PHP script. To include the script, different types of functions are available, those are :-

  • include

  • include_once

  • require

  • require_once

include :

This function includes a file 'n' number of times, if the included file is not available, it returns a warning message and executes rest of the statements.

Example :


<?php echo "From Page1"; include "Page2.php"; echo $n; echo constants("City"); fn(); ?>


<?php echo "From Page2"; $n = 100; define("City", "NY"); function fn() { echo "This is from Function"; } ?>

include_once :

It is same as include but it includes external file only one time. First it will check whether the specified file already included or not. If it is not included, then only it includes external file.

Example :

<?php echo "From Page1"; include_once "Page2.php"; include "Page2.php"; ?>

require :

It is same as include but it returns fatal errors if external file is not available & stops the execution of script.

Example :

<?php echo "From Page1"; require "Page2.php"; echo "NEXT"; ?>

require_once :

It is same as require but only one time it includes the external file.

Example :

<?php echo "From Page1"; require_once "Page2.php"; ?>