PHP include Statement

PHPServer Side ProgrammingProgramming

Introduction

PHP code in one file can be made available for use in another PHP script with the help of include statement. This feature enables modular program development by writing functions and classes once and calling wherever needed.

The include statement checks for the file in current folder by default and further in directories mentioned in include_path setting of php.ini. If file asked for is not available in current folder as well as include_path folders, PHP parser emits E_WARNING and execution of calling module continues.

The included file can access variables defined in calling module prior to inclusion. Such variables will have global scope.

include Example

In following example main php script includes test.php

Example

 Live Demo

<?php
echo "inside main script\n";
$var1=100;
echo "now calling test.php script\n";
include "test.php";
echo "returns from test.php";
?>
//test.php
<?php
$var2=200;
//accessing $var1 from main script
echo $var1+$var2 . "\n";
?>

Output

This will produce following result when main script is run from command line −

inside main script<br />now calling test.php script<br /><br />300<br />returns from test.php

Warning for failed include

In following example,attempt to include nonexisting file results in warning

Example

 Live Demo

<?php
echo "inside main script\n";
$var1=100;
echo "now calling nosuchfile.php script\n";
include "nosuchfile.php";
echo "returns from nosuchfile.php";
?>

Output

This will produce following result. Note that program doesn't teerminate on warning −

inside main script
now calling nosuchfile.php script
PHP Warning: include(nosuchfile.php): failed to open stream: No such file or directory in line 5
PHP Warning: include(): Failed opening 'nosuchfile.php' for inclusion (include_path='C:\xampp\php\PEAR') in line 5
returns from nosuchfile.php

include from inside function

In next example, test.php is included inside a function. Code inside included file will be treated as part of function. Hence variables in the include file will not accessible outside the function

Example

 Live Demo

//main script
<?php
function myfunction(){
   $var1=100;
   include "test.php";
   echo $var1+$var2."\n";
}
myfunction();
echo "variable from included file outside function: $var2";
?>
//test.php included
<?php
echo "test.php called from inside function\n";
$var2=200;
return;
?>

Output

This will produce following result when main script is run from command line−

100
test.php called from inside function
PHP Notice: Undefined variable: var2 in C:\xampp\php\testscript.php on line 9
Notice: Undefined variable: var2 in C:\xampp\php\testscript.php on line 9
variable from included file outside function

return from include file

The include statement returns TRUE on success and FALSE on failure. If the include file explicitly returns an expression, it can be used by calling module for further processing.

Example

//main script
<?php
$result=include "test.php"; // returns value
echo "retun value : $result\n";
$y=include "test1.php"; //with return only
echo "return value : $y\n";
$x=include "test2.php"; //no return statement
echo "return value : $x\n";
?>
//test.php
<?php
$var=100;
return $var;
?>
//test1.php
<?php
$var=100;
return;
?>
//test2.php
<?php
$var=100;
?>

Output

This will produce following result −

retun value : 100
return value :
return value : 1
raja
Published on 18-Sep-2020 16:30:06
Advertisements