To include a PHP file into an opened PHP script we may use one of following two possible groups of statements:

  1. The require and require_once statements
  2. The include and include_once statements

The include statement includes a file that is specified in the script. The path of the file can also be given if the file does not belong to the same folder.

NOTE: The difference between the include and require statements is that the include statement in case of a failure will produce a warning and continue with the script, while the require statement produces an error and halts the script.

The include_once statement is similar to the include statement except the script will check if the file has already been included in the script, for instance:

<?php
    include_once "file.php"; // file added into script
    include_once "File.php"; // file NOT added into script
?>

Syntax for INCLUDE statement

<?php
    require "file.php";
?>

Examples with INCLUDE and INCLUDE_ONCE statements

<?php
// file.php stored somewhere on server
    $name = "Eduardo";
    $age = 37;
?>

<?php
// main.php stored somewhere on server and includes file.php
    echo "Person's name and age: ";
    
    include "file.php";
    
    echo $name . ", " . $age;
// output is 'Person's name and age: Eduardo, 37
?>