include()

The include() statement includes and evaluates the specified file.

An important note about how this works is that when a file is include()ed or require()ed, 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.

This happens each time the include() statement is encountered, so you can use an include() statement within a looping structure to include a number of different files.

  1 
  2 $files = array ('first.inc', 'second.inc', 'third.inc');
  3 for ($i = 0; $i < count($files); $i++) {
  4     include $files[$i];
  5 }
  6      

include() differs from require() in that the include statement is re-evaluated each time it is encountered (and only when it is being executed), whereas the require() statement is replaced by the required file when it is first encountered, whether the contents of the file will be evaluated or not (for example, if it is inside an if statement whose condition evaluated to false).

Because include() is a special language construct, you must enclose it within a statement block if it is inside a conditional block.

  1 
  2 /* This is WRONG and will not work as desired. */
  3  
  4 if ($condition)
  5     include($file);
  6 else
  7     include($other);
  8  
  9 /* This is CORRECT. */
 10  
 11 if ($condition) {
 12     include($file);
 13 } else {
 14     include($other);
 15 }
 16      

In both PHP3 and PHP4, it is possible to execute a return statement inside an include()ed file, in order to terminate processing in that file and return to the script which called it. Some differences in the way this works exist, however. The first is that in PHP3, the return may not appear inside a block unless it's a function block, in which case the return applies to that function and not the whole file. In PHP4, however, this restriction does not exist. Also, PHP4 allows you to return values from include()ed files. You can take the value of the include() call as you would a normal function. This generates a parse error in PHP3.

Example 11-1. include() in PHP3 and PHP4

Assume the existence of the following file (named test.inc) in the same directory as the main file:
  1 
  2 <?php
  3 echo "Before the return <br>\n";
  4 if (1) {
  5     return 27;
  6 }
  7 echo "After the return <br>\n";
  8 ?>
  9      

Assume that the main file (main.html) contains the following:
  1 
  2 <?php
  3 $retval = include ('test.inc');
  4 echo "File returned: '$retval'<br>\n";
  5 ?>
  6      

When main.html is called in PHP3, it will generate a parse error on line 2; you can't take the value of an include() in PHP3. In PHP4, however, the result will be:

Before the return
File returned: '27'
     

Now, assume that main.html has been altered to contain the following:
  1 
  2 <?php
  3 include ('test.inc');
  4 echo "Back in main.html<br>\n";
  5 ?>
  6      

In PHP4, the output will be:

Before the return
Back in main.html
     
However, PHP3 will give the following output:

Before the return 
27Back in main.html

Parse error: parse error in /home/torben/public_html/phptest/main.html on line 5
     

The above parse error is a result of the fact that the return statement is enclosed in a non-function block within test.inc. When the return is moved outside of the block, the output is:

Before the return
27Back in main.html
     

The spurious '27' is due to the fact that PHP3 does not support returning values from files like that.

Please note that both include() and require() actually pull the contents of the target file into the calling script file itself; they do not call the target via HTTP or anything like that. So any variable set in the scope in which the inclusion happens will be available within the included file automatically, since it has effectively become a part of the calling file.

  1 
  2 include ("file.inc?varone=1&vartwo=2"); /* Won't work. */
  3 
  4 $varone = 1;
  5 $vartwo = 2;
  6 include ("file.inc");  /* $varone and $vartwo will be available in file.inc */
  7      

Don't be misled by the fact that you can require or include files via HTTP using the Remote files feature; the above holds true regardless.

See also readfile(), require(), and virtual().