include require
suggest changerequire
require is the same as include, except that a missing file also throws an Error after a warning about the open. An uncaught Error stops the script. On current PHP that failure is catchable. Older manuals call it a fatal E_COMPILE_ERROR. A failed include emits E_WARNING, returns false, and the script continues.
require 'file.php';
PHP Manual - Control Structures - Require
include
The include statement includes and evaluates a file.
./variables.php
$a = 'Hello World!';
./main.php
include 'variables.php';
echo $a;
// Output: `Hello World!`
Be careful with this approach. It is a code smell because the included file changes which variables exist in the caller's scope.
An included file can return a value. That is a useful way to load a configuration array:
configuration.php
<?php
return [
'dbname' => 'my db',
'user' => 'admin',
'pass' => 'password',
];
main.php
<?php
$config = include 'configuration.php';
This approach will prevent the included file from polluting your current scope with changed or added variables.
PHP Manual - Control Structures - Include
The value returned by the included file can be assigned. Variables assigned inside that file, such as $a below, still appear in the caller's scope.
Example :
include1.php file :
<?php
$a = "This is to be returned";
return $a;
?>
index.php file :
$value = include 'include1.php';
// Here, $value = "This is to be returned"