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.| CARVIEW |
Select Language
HTTP/2 301
server: myracloud
date: Tue, 03 Feb 2026 05:16:20 GMT
content-type: text/html
content-length: 161
location: https://www.php.net/manual/en/function.require.php
HTTP/2 200
server: myracloud
date: Tue, 03 Feb 2026 05:16:21 GMT
content-type: text/html; charset=utf-8
content-language: en
permissions-policy: interest-cohort=()
x-frame-options: SAMEORIGIN
link: ; rel=shorturl
last-modified: Tue, 03 Feb 2026 04:08:00 GMT
vary: accept-encoding
content-encoding: gzip
expires: Tue, 03 Feb 2026 05:16:21 GMT
cache-control: max-age=0
PHP: require - Manual
update page now
require
(PHP 4, PHP 5, PHP 7, PHP 8)
require is identical to include
except upon failure it will also produce an Error
exception (E_COMPILE_ERROR level error prior to
PHP 8.0.0) whereas include will only produce a warning
(E_WARNING level error).
See the include documentation for how this works.
+add a note
User Contributed Notes 3 notes
chris at chrisstockton dot org ¶
18 years ago
Marcel Baur ¶
4 years ago
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"
jave dot web at seznam dot cz ¶
2 years ago
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.
↑ and ↓ to navigate •
Enter to select •
Esc to close • / to open
Press Enter without
selection to search using Google