PHP: Hypertext Preprocessor (original) (raw)
require_once
(PHP 4, PHP 5, PHP 7, PHP 8)
The require_once
expression is identical torequire except PHP will check if the file has already been included, and if so, not include (require) it again.
See the include_once documentation for information about the _once
behaviour, and how it differs from its non _once
siblings.
Found A Problem?
13 years ago
`If your code is running on multiple servers with different environments (locations from where your scripts run) the following idea may be useful to you:
a. Do not give absolute path to include files on your server.
b. Dynamically calculate the full path (absolute path)
Hints:
Use a combination of dirname(FILE) and subsequent calls to itself until you reach to the home of your '/index.php'. Then, attach this variable (that contains the path) to your included files.
One of my typical example is:
instead of:
After this, if you copy paste your codes to another servers, it will still run, without requiring any further re-configurations.
[EDIT BY danbrown AT php DOT net: Contains a typofix (missing ')') provided by 'JoeB' on 09-JUN-2011.]
`
bobray99 at softville dot com ¶
2 years ago
`Be careful when using include_once and require_once for files that return a value:
fiddle2.php
<?php
return "Some String";fiddle.php
<?php
$s = require_once('fiddle2.php');
echo "\n" . $s;
$s = require_once('fiddle2.php');
echo "\n" . $s;/* output
Some String
1
*/The second time require_once occurs, it returns 1 because the file has already been included.
`
boda0128318 at gmail dot com ¶
4 years ago
`1 - "require" and "require_once" throw a fatal error if the file is not
existing and stop the script execution
2 - "include" and "include_once" throw a warning and the execution
continues
3 - "require_once" and "include_once" as their names suggests ,
they will not include the file if the file was already included with
"require", "require_once", "include" or "include_once"
try the following code:
create a file called "index.php"
and another file that is called "first.php" and write the following header
Hello every one
i hope this will help you
`
9 years ago
"require_once" and "require" are language constructs and not functions. Therefore they should be written without "()" brackets!