Contents / Previous / Next


File Including: include and require

Storing (HTML-)text and (PHP-)code in separate file and including it when needed is useful in building dynamic web pages, and a good way to modularize and organize code.

The include() and require() statements includes and evaluate the specified file.
The two constructs are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error.
Use require() if you want a missing file to halt processing of the page.

Example:

<html> <title>PHP Includes</title> <? include ("includes/simple.txt") ?> </html>

You can specify the include_path in the php.ini configuration file, default is:

include_path=".:/php/includes"
Or provide an absolute path to the file you want to include.
If "URL fopen wrappers" are enabled (in php.ini, default), you can also specify an URL.

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags. Example:

<? $condition = true; if ( $condition ) { include("include_file.php"); } else { include("my_text.html"); } ?> You must enclose them within a statement block if it is inside a conditional block.
This may be the "include_file.php": <?php echo "This was an included PHP script"; ?>


Include and Variable Scope: When a file is included, the code it contains inherits the variable scope of the line on which the include occurs.


Handling Return Values: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it.
The return value from the included file is the returned by the include() function..
Example (include and return):

<?php // This is the include file "return.php" $var = 'PHP'; return $var; ?> <?php // This is the include file "noreturn.php" $var = 'PHP'; ?> <?php // This is the main file "testreturns.php" $foo = include 'return.php'; echo $foo; // prints 'PHP' $bar = include 'noreturn.php'; echo $bar; // prints 1 ?> $bar is the value 1 because the include was successful.


include_once() and require_once()

The only difference between include_once()/require_once() and include()/require() is that if the code from a file has already been included, it will not be included again (as the name suggests).

include_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.


eval -- Evaluate a String as PHP Code

An alternative to "include" files is to load files into string-variables with fopen(), file() with the output-control functions or from a database text field and to evaluate the string with the eval() function: mixed eval ( string code_str) eval() evaluates the string given in code_str as PHP code.
Variables given values under eval() will retain these values in the main script afterwards.
eval() returns NULL unless return() is called in the evaluated code,
Example (simple text merge): <?php $string = 'cup'; $name = 'coffee'; $str = 'This is a $string with my $name in it.<br>'; echo $str; eval ("\$str = \"$str\";"); echo $str; //output: //This is a $string with my $name in it. //This is a cup with my coffee in it. ?> <P><HR></P> </BODY> </HTML>