PHP include and require

When another PHP file is included using include or require keyword, all variables which were declared in another PHP file that is being included will become available from that point forward, from the line where the include statement was used.

//game.php $name = 'The Witcher 3'; $cost = 35.60; $developer = 'CD Projekt RED'; //welcome.php echo "Welcome to our PC video game review web site"; include( 'game.php' ); //game.php's variables become available from this point forward echo "Today's featured game is $name, developed by $developer. You may purchase the game at amazon.com for $$cost."; //output: //Today's featured game is The Witcher 3, developed by CD Projekt RED. You may purchase the game at amazon.com for $35.60.

Including File with Functions and Classes

All variables will have variable scope from the line they are being included. However, functions and classes which are defined in the file that is being included have global scope, meaning they'll be available anywhere in the program, including inside other functions and classes.

//game.php function message ( ) { echo "Welcome to our PC review web site!"; } $game = array('name' => 'The Witcher 3', 'cost' => 35.60, 'developer' => 'CD Projekt RED'); //welcome.php include ( 'game.php' ); print_r( $game ); /*output: ( global scope ) Array ( [name] => The Witcher 3 [cost] => 35.6 [developer] => CD Projekt RED ) */ function welcome( ) { print_r ( $game ); //output: Undefined variable: game ( local scope ) message ( ); //output: Welcome to our PC review web site! } welcome( );

So, from the example above you can see that $game variable is available outside the function, but is not accessible within the welcome( ) function. However, the message( ) function is available from within welcome( ) function, because all functions and classes that are being included have global scope.

Including From Within a Function

When a file is included from within a function, all variables that are defined in the file that is being included behave as if though they have been declared inside that function, so they will have local scope of that function, that is unless we use keyword global on the variable that is being included.

//game.php $name = 'The Witcher 3'; $developer = 'CD Projekt RED'; //welcome.php function welcome ( ) { global $developer; //will have access outside of function include 'game.php'; echo "$name by $developer"; } welcome(); //output: The Witcher 3 by CD Projekt RED echo "$name by $developer"; //output: by CD Projekt RED

Include vs Require

When using include statement, the script will continue to run if the file is not found. Use when file is not required by the app.

<html> <body> <?php include('file-not-found.php'); //program continues running ?> </body> </html>

Using require, however, will terminate the script if the file is not found. Use when file is absolutely required by the application.

<html> <body> <?php require('file-not-found.php'); //Fatal error, program stops ?> </body> </html>

Include From Anywhere

As you move your project files around, it may become messy and time consuming, having to change your include paths every time you move your files. For that, there is a simple script which uses the $_SERVER global array and the DOCUMENT_ROOT key which gets the current working root directory:

//code credit: snowyurik at gmail dot com include $_SERVER['DOCUMENT_ROOT']."/lib/sample.lib.php";