The include Function

The include function includes and executes the specified file. If the file is not found, a warning is issued, but script execution continues. The function accepts the file path as a parameter.

Syntax

phpinclude 'path/to/file.php';

Example

Let's include the file header.php and output its contents:

php<?php
  include 'header.php';
?>

Example

Let's try to include a non-existent file:

php<?php
  include 'nonexistent.php';
  echo "Script continues...";
?>

Code execution result:

phpWarning: include(nonexistent.php): failed to open stream: No such file or directory
Script continues...

Example

Let's include a file with variables and use them:

php<?php
  // config.php contains: $siteName = "My Site";
  include 'config.php';
  echo $siteName;
?>

Code execution result:

php'My Site'

See Also

  • the require function,
    which is similar to include, but causes a fatal error if the file is missing
  • the include_once function,
    which includes the file only once