include

The include statement includes and evaluates the specified file.

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 file.

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

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.

 /* This is WRONG and will not work as desired. */
 
 if ($condition)
     include($file);
 else
     include($other);
 
 /* This is CORRECT. */
 
 if ($condition) {
     include($file);
 } else {
     include($other);
 }
      

When the file is evaluated, the parser begins in "HTML-mode" which will output the contents of the file until the first PHP start tag (<?) is encountered.

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