PHP: Hypertext Preprocessor (original) (raw)

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](errorfunc.constants.php#constant.e-compile-error) level error prior to PHP 8.0.0) whereas include will only produce a warning ([E_WARNING](errorfunc.constants.php#constant.e-warning) level error).

See the include documentation for how this works.

Found A Problem?

chris at chrisstockton dot org

17 years ago

`Remember, when using require that it is a statement, not a function. It's not necessary to write:

The following:

Is preferred, it will prevent your peers from giving you a hard time and a trivial conversation about what require really is.`

Marcel Baur

3 years ago

`If your included file returns a value, you can get it as a result from require(), i.e.

foo.php:

$bar = require("foo.php");
echo $bar; // equals to "foo"

`

jave dot web at seznam dot cz

1 year 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:

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.

`