To include a PHP file into an opened PHP script we may use one of following two possible groups of statements:
- The require and require_once statements
- 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.
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
?>
Comments
No comments have been made yet.
Please login to leave a comment. Login now