require()

The require() statement includes and evaluates the specific file.

require() includes and evaluates a specific file. Detailed information on how this inclusion works is described in the documentation for include().

require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.

Пример #1 Basic require() examples

<?php

require 'prepend.php';

require 
$somefile;

require (
'somefile.txt');

?>

See the include() documentation for more examples.

Замечание: Prior to PHP 4.0.2, the following applies: require() will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.

Замечание: Поскольку это языковая конструкция, а не функция, она не может вызываться при помощи переменных функций

Внимание

Версии PHP для Windows до PHP 4.3.0 не поддерживают возможность использования удаленных файлов этой функцией даже в том случае, если опция allow_url_fopen включена.

See also include(), require_once(), include_once(), get_included_files(), eval(), file(), readfile(), virtual() and include_path.

Коментарии

Remember, when using require that it is a statement, not a function. It's not necessary to write:
<?php
 
require('somefile.php');
?>

The following:
<?php
require 'somefile.php';
?>

Is preferred, it will prevent your peers from giving you a hard time and a trivial conversation about what require really is.
2007-06-19 20:06:58
http://php5.kiev.ua/manual/ru/function.require.html
Автор:
If your included file returns a value, you can get it as a result from require(), i.e.

foo.php:
<?php
return "foo";
?>

$bar = require("foo.php");
echo $bar; // equals to "foo"
2021-07-27 12:08:19
http://php5.kiev.ua/manual/ru/function.require.html
Always use __DIR__ to define path relative to your current __FILE__. 
(Or another setting that is originally based on __DIR__/__FILE__)

try & catch - don't get confused by the words "fatal E_COMPILE_ERROR" - it's still just an internal Error that implements Throwable - it can be caught:

<?php
try {
    require(
__DIR__ '/something_that_does_not_exist');
} catch (
\Throwable $e) {
    echo 
"This was caught: " $e->getMessage();
}
echo 
" End of script.";
?>

Note that this will still emit a warning "Failed to open stream: No such file or directory..." ...unless you prefix the require with "@" - which I strongly don't recommend as it would ignore/supress any diagnostic error (unless you have specified set_error_handler()). But even if you'd prefix the require with "@" it would still be caught.
2024-01-15 16:29:01
http://php5.kiev.ua/manual/ru/function.require.html

    Поддержать сайт на родительском проекте КГБ