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
include 'path/to/file.php';
Example
Let's include the file header.php
and output its contents:
<?php
include 'header.php';
?>
Example
Let's try to include a non-existent file:
<?php
include 'nonexistent.php';
echo "Script continues...";
?>
Code execution result:
Warning: 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
// config.php contains: $siteName = "My Site";
include 'config.php';
echo $siteName;
?>
Code execution result:
'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