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:
<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/config.php');
?>
instead of:
<?php require_once('/var/www/public_html/config.php'); ?>
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.]
PHP.mk документација
require_once
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Патека
function.require-once.php
Локална патека за оваа страница.
Извор
php.net/manual/en
Оригиналниот HTML се реупотребува и локално се стилизира.
Режим
Прокси + превод во позадина
Кодовите, табелите и белешките остануваат читливи во истиот тек.
Референца
function.require-once.php
require_once
Референца за `function.require-once.php` со подобрена типографија и навигација.
require_once
(PHP 4, PHP 5, PHP 7, PHP 8)
На require_once изразот е идентичен со
require освен што PHP ќе провери дали датотеката веќе е вклучена, и ако е така, нема да ја вклучи (бара) повторно.
Постојат голем број корисни функции за include_once документација за информации за _once однесувањето, и како се разликува од неговиот не _once siblings.
Белешки од корисници 4 белешки
бимал на санјаал точка ком ¶
пред 14 години
бобрај99 на софтвил точка ком ¶
3 години пред
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.
бода0128318 на џимејл точка ком ¶
пред 4 години
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"
<?php
require "first.php"; // this will include the file
include_once "first.php"; // this will not as it was included using "require"
require_once "first.php"; // this will not as it was included using "require"
?>
and another file that is called "first.php" and write the following header
-------------------------------
<h1>Hello every one</h1>
--------------------------------
i hope this will help you
nepomuk at nepda dot de ¶
пред 10 години
"require_once" and "require" are language constructs and not functions. Therefore they should be written without "()" brackets!