require

require is similar to include, except that it will produce a fatal E_COMPILE_ERROR level error on failure. When the require fails, it will halt the script. When the include fails, it will not halt the script and only emit E_WARNING.

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, since it is considered a code smell, because the included file is altering amount and content of the defined variables in the given scope.


You can also include file, which returns a value. This is extremely useful for handling configuration arrays:

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