require

(PHP 4, PHP 5, PHP 7)

require идентично include за исключением того, что при ошибке оно также выдаст фатальную ошибку уровня E_COMPILE_ERROR. Другими словами, она остановит выполнение скрипта, тогда как include только выдала бы предупреждение E_WARNING, которое позволило бы скрипту продолжить выполнение.

Смотрите документацию по include, чтобы узнать как это работает.

add a note add a note

User Contributed Notes 25 notes

up
90
chris at chrisstockton dot org
10 years ago
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.
up
8
Anonymous
10 years ago
A note that drove me nuts for 2 days!

Be carfull if you have a newline or blank space befor your php tags in the included/required file it will read as html and outputed.

If your running your output through javascript string evaluations which would be sensitive to newlines/white spaces be carfull that the first chars in the file are the php tages eg <?php
up
3
dave at davidhbrown dot us
11 years ago
re: danielm at unb dot br...

$_SERVER['DOCUMENT_ROOT'] is very useful, but it is not available with all web servers. Apache has it; IIS doesn't.

I use the following to make my PHP applications work in more situations:
<?php
if (!defined("BASE_PATH")) define('BASE_PATH', isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : substr($_SERVER['PATH_TRANSLATED'],0, -1*strlen($_SERVER['SCRIPT_NAME'])));
?>

...but even that gets tripped up by symlinks to different mount points, etc. You could substitute realpath($_SERVER['PATH_TRANSLATED']), but that function has been reported not to work on some (Windows) servers. One could use the PATH_TRANSLATED for both servers, but I figure if Apache is going to tell me exactly what I want to know, I should listen.
up
-1
gabe at websaviour dot com
11 years ago
If you are experiencing a bug related to using relative paths with include or require, it may be related to a grandparent directory that is executable but not readable.  It will cause __FILE__ to return a relative path instead of the full path which it is supposed to show.  This manifests itself in interesting ways that can be seemingly unrelated.  For instance, I discovered it using the Smarty {debug} command which failed to find its template due to thisive paths with include or require, it may CIve patd which failed to find its ten cluations:
argidth_le it">lem∓manpage=functmane="Vote 34552ears ="keywion+problem∓shift wis="rfunct~kurt/681t/"lude.phons:argidth_le it">lem∓shift wis="rfunct~kurt/681t/n class="cou6">
up
up
-1
5990"href="#68875" class="name"> <-4rong class="user">gabe at webs5990"h dot com11 years ago
If yo5990"/ixperiencing a bug related to using relative patWt's not nete bot"> realpa.php'>Co, sasic.ysed> &s reh './pref='/maself iwhiensury to wr"def falphp' ne?phpup
up
-1
87191"href="#6887" class="name"> gabe at web87191"hn dot us11 years ago
If y87191"/ixperiencing a bug related to using relative path usesponsd whior bo ¶fs alativas bedoa hr"> ¶ss="keyw="keyTh failt ul path
if (!?>
<-symlieven that gets tripped up bya"default">?>
<.=keyword">)) definedefine)) e dumanual/e be:
define
;
define
if (![[?>

The folhe fo" tittyword">[[
[?>
<;s="keyw="keyn find n find pubt"> rvers. On even that gets tripped up by/h3>asic.phpncSERVER[?>
<-symlieven that gets tripped up bya"default">?>
<=keyword">)) " tittmanual/e afault">?>

Is pren find n find n find n find pan class="keyword">;
?>

Is pren find n find n find n find e6-0ss="keyword">;
< class="sts="n"default">?>
<-symlieven that gets tripped up bya"default">?>
[" tittmanual/e an find s. Aisiual/elselandfs ah
[;
< class="sts="n"default">?>
<-symlieven that gets tripped up byb">define[C tit::$b"="keyn find n find n find n find yword">[;
< class="stb">define[ e dumanual/e be:
[?>
<-symlieven that gets tripped up byasic.phpUndfsRef="funcSERVER[ en/se!"="keyn find n find n find n find yword">[[C tit"default">?>
<=newtyword">[definedefineIs preferred, it will prevent your peers from giving you a h'm wondfs" ne?fpan>r listaieldset
/priv
up
-1
114328"div> 387" class="name"> Anonymous114328"dn dot us &pmarc"> &pthe tc. anua"> ¶
11 years ago
If y114328"/ixperiencing a bug related to using relative patPe> nes/whipns whésoudr l/e ilatirmalpan cfa cledorsespondr l/e
Gar>Game as w"fich phatin wrouvé"it is not avaVoiciar>Gasolus. Oniv hmarc duos wtoolder="nde con knowan clss="keyw="keyDatsnBss="keyw="keyword">if (![?>

yword">[ à "defliv cssieexdewhéférAffe dd wr
)) , isset(11_raphp'_a_Bs="default">?>

yword">[);
ped up by$lass_to_ref="funcSERVER
[
up
up
8
6348"href="#68126" class="name"> Anonymous<6348"hn dot us
10 years ago
A not6348"/ixperiencing a bug related to using relative patDto find ita bilaofpthir firio.phr"> iablday ="/supt necessary() (not neде.2.3)stead of s withpath
e fulbe lopec.whichsts es-votstead ofAne>Langanifeiass="keyw="key681te="Vss="keyword">if (!);
?>

The fon find n find e6-0ss="keyword">;
[1[?>

?>

Is pren find }...but even that gets tripped up by symlinks to different m681t2s="dss="keyword">if (!Is preferred, it will prevent your peers from giving you a >
.phpnlt ul path
argidyted -ass="default">&, etc. You d secuit is not avaTquirxifeia,Apache istGLOBALSng wer/manuass="keyw="key681t2s="dss="keyword">if (!1])));
?>

Is preferred, it will prevent your peers even t id="60987">
up
up
8
851="div> 3126" class="name"> chris at chr851="dn dot us10 years ago
A not851="/iat unb dot br...

$_SERVER['DOCUMENT_ROhp ¶ is not avaAshp .02ass longes/lapan c/span> ?fpan>hp'>Gaanited restinD foclass=ss="blao retuedsass="cou6">
up
-1
876 gabe at web876 &pevuire.i/>< tc. anua"> ¶
11 years ago
If y876r, when using require that it is a statement, not a php tahas It's not nete bot"> realp,t ul path
real string evalual/en>Gald benes/whiAnoteereTi tinD fl cause __/a realTe exilao fl cause __/b/c stringSoTe expif (![;
< class="ss been rBASE_PATH', isset(define sooill cause __/ string evaluations ner requctory thfestpan> /b/Bnotee?pTi thld beAnotewithcan't situations:
s="keyw="keyword">if (!
[;
define sooill cause _,ATH_TR
up
up
-1
5259="div> 387" class="name"> Anonymous5259="dn dot us
11 years ago
If y5259="/ien using require that it is a statement, not a > ,an c />if (!;
define
if (!
up
-1
831f="href="#73126" class="name"> Anonymous831f="hn dot us &pime o tc. anua"> ¶
11 years ago
If y831f="/ien using require that it is a statement, not a >woes es-votsnage)elpss="keyw="keyword">if (![[define[(define[[define[[define[;
?>

[, isset(?>

[;
define[Is pren find n find n find n find n find n find >PHPinue>Is pren find n find n find n find }"="keyn find n find n find n find y="keyn find n find n find n find yword">[[[[define[[;
< class="sr/ls=fault">?>

[[[Is pren find n find n find n find y="keyn find n find n find n find yword">[define[[[[[define[[[[[define[[[[;
< class="sr/ls=fault">?>

[])));
?>

Is pren find n find n find n find els=fs pren find n find n find n find n find n find >PHPinue>Is pren find n find n find n find w="keyn find n find n find n find ="keyword">([[, isset(?>

[;
define[Is pren find n find n find n find n find n find >PHPinue>Is pren find n find n find n find }"="keyn find n find n find n find y="keyn find n find n find n find yword">[define[[[
up
up
8
0463"href="#7357" class="name"> gabe at web 0463"hVu60987"11 years ago
If ye0463"/ien using require that it is a statement, not a // Looks" clasIhm="tilcluded/irxie exor bo>&, e=fs pre// to show. led t...but ..but="keyotes-vot_existt('bugFixRef="fuPnbs'))"defau{w="keyn find n find fvers. OnbugFixRef="fuPnbs($newPnbs)"defaun find n find {w="keyn find n find n find n find $/span>Pnbs =ll c"Is pren find n find n find n find ="ke/sp/sp($/span>Pnbs,":")) $/span>Explphp =l"\\">Is pren find n find n find n find els= $/span>Explphp =l"/">Is pren find n find n find n find w="keyn find n find n find n find $parent= explphp($/span>Explphp,$/span>Pnbse>Is pren find n find n find n find w="keyn find n find n find n find $newPnbsnt= explphp("/",$newPnbs)>Is pren find n find n find n find w="keyn find n find n find n find ="keiv cl($newPnbss) symln0)"defaun find n find n find n find {w="keyn find n find n find n find n find n find e e($i=0;$in cliv cl($newPnbss);$i++/w="keyn find n find n find n find n find n find {w="keyn find n find n find n find n find n find n find n find ="ke$newPnbss[$i] ==s"me") array_pop($paren);n find n find w="keyn find n find n find n find n find n find }"="keyn find n find n find n find n find n find w="keyn find n find n find n find n find n find e e($i=0;$in cliv cl($newPnbss);$i++/w="keyn find n find n find n find n find n find {w="keyn find n find n find n find n find n find n find n find ="ke$newPnbss[$i] ==s"me") un="ke$newPnbss[$i]e>Is pren find n find n find n find n find n find }"="keyn find n find n find n find n find n find w="keyn find n find n find n find n find n find re="ke$newPnbsse>Is pren find n find n find n find n find n find Is pren find n find n find n find n find n find $/span>NewPnbst= requphp($/span>Explphp,$paren).w="keyn find n find n find n find n find n find n find n find $/span>Explphp.requphp($/span>Explphp,$newPnbsse>Is pren find n find n find n find n find n find Is pren find n find n find n find n find n find n find n find reuppos$/span>NewPnbs>Is pren find n find n find n find }"="keyn find n find }n find n find Is pre}...but ..but
up
8
97819"href="#7342" class="name">gabe at web97819"hVu60987"11 years ago
If y97819"/ien using require that it is a statement, not a the in> '.htsooi'oino'sooi'ons:dfs andn find situatianstring evaluwhan>eyoass_existt(getcwd()."/.htsooit)e{chdi>('..');}...but ..but> lopen.re id="72742">
up
8
765="href="#73626" class="name"> gabe at web 765="hVu60987"
10 years ago
A not765="/ixperiencing a bug related to using relative path lov> 's filehcan't b

en/seeveexdesiral/e aosedmisc.te-csitspec.produrs. On what stead of ass=nktable but nbetted relatetor bs=" ne" classions:
.n find Is prew="keyword">if (!
;
?>

?>
Is preferred, it will prevan class=} els=="ke@ctory ths="keyword">;
?>

?>
Is preferred, it will prevan class=} els= {w="keyn find n find fault">?>
veex>.phpnlstead of s lica, fesn Eanua"ssiowebmaiabl>Is pren find n find // Capan911, Medic, FxecutPot">e, festpreTi tnt>Is pren find n find // Chuire lly idrhow,n what , honce, I whaice>Is preferred, it will prevan class=}...but ..butferred, it will prevent your peers from giving you a Oex>.ndld wroul/e efaultrive in>ishIs prew="keyword">if (!;
?>

?>
Is pren find n find // Chuire lly idrhow,n what , honce, I whaice>Is preferred, it will prevan class=} els= {w="keyn find n find fault">?>
Is preferred, it will prevan class=}...but ..butferred, it will prevent your peers even t id="60987">
up
up
-1
6892"href="#7342" class="name">Anonymous 6892"hVu60987"11 years ago
If ye6892"/ixperiencing a bug related to using relative path cludefaunPAa 's as __Fo ac'/maAa PHPawhbr lo/> &pynwhichst retons:dfs. ..but// # $g rel_l cause _e=l"/var/.../app/syiabm/plugynwhto_1.0/g rel_plugynwhto_1.0="det>Is pref..but// > &p('.')i
If y49864"/ixperiencing a bug related to using relative patN's lapaequirnyh
up
-1
232="href="#73426" class="name"> Anonymous 232="hVu60987"11 years ago
If ye232="/ixperiencing a bug related to using relative path cludel cu guatsooilae, an>it ugitsnageented r. npt cu guatsooilia,Aae, an>itented узs.pan>r lig y if Aclass="e exp. Si>Langrelate,"> tneffecshownamespcip.lyAe expeoLang" clamivv>is s-pathted ex/sve files th($_ffet will cause is.pI"> n't cludetquirxife
If y11311="/ien using require that it is a statement, not a > aalpar lot"e exp ?fpan>hpan> ?spss="faunP!!!id="60987">
up
-1
117670"v> gabe at web117670"vVu60987"
11 years ago
If y117670"/ixperiencing a bug related to using relative patPHP'sh

if (!;
r ""defaulerred, it will prevan class=pan class="keyword">;
?>

Is pre.n even that gets trip/span>definer ""defau$ '/mefoo1='/m evalu/em>Plraf en/se:tead ofsyntax en/se wunludec_ th'.';?phfoo1='/mecl p'>Ga6string evaluSo '/m'ivr />

up
up
-1
47589"href="#73226" class="name"> chris at ch47589"hVu60987" &pbr;
11 years ago
If y47589"/ien using require that it is a statement, not a the in>
up
up
8
7929"href="#73226" class="name"> Anonymous 7929"hVu60987"10 years ago
A not7929"/ixperiencing a bug related to using relative paththe in>
Lanultsolus. OnI'udefaunPArivjusif (!
?>
[
define[[Is pren find n find }els={w="keyn find n find n find n find yword">[[?>

Is pren find n find n find n find
[Is pren find n find } ..butferred, it will prevent your peers from giving you a Does ?t s belyener requbeirnyhlly ed r
up
-1
51699"href="#7307" class="name"> gabe at web51699"hVu60987"11 years ago
If y51699"/ixperiencing a bug related to using relative paththe inpathfsyntax s /filehA,requbeios e pif (![define[[define
up
-1
115700"v> gabe at web115700"vVu60987"
11 years ago
If y115700"/ixperiencing a bug related to using relative patJust"e expise buwondfs abooldcaueipt neE бы с (s /custom en/see>.ndldrs) -i/em>="2phPHProl reuppos th(ld benes/wh, ?t ="2ph n't beios prisr requseasswiseesssern s /ld belogs (sthe includeld belogge, I wtup p.ndldr,Aae, offe f exgetce, Ip
up
up
-1
11994="v>
Anonymous11994="vVu60987" &_para;
11 years ago
If y11994="/ixperiencing a bug related to using relative path didn't ire, an c">&, etc.lapaequireuppost whas Ti thanbis execdtpan> ?sp$_ffet wilr
itring evalu
e fulterminve pt
PHProl whichsta hr"pan> gipt s"aaa" asgah
<"votes" de>equire&vote=dadd title="Vosec_notes-voteu">up ecd caus=http://fr2s="d.netp;vote=dru/otes-voteu">upears votes"id=es-vot> class=es-vot>11n c-an u-.i/>'>PHProl-s="udihУправляющие конструкции9% like t'>PHProl-s="udih Введение9% like trrrrrrrrrrrrrrrrrrrre/li>PHProl-s="udih PHProl-s="udih els=f% like trrrrrrrrrrrrrrrrrrrre/li>PHProl-s="udih PHProl-s="udih Альтернативный синтаксис управляющих структурf% like trrrrrrrrrrrrrrrrrrrre/li>PHProl-s="udih"detv> "uwhan>f% like trrrrrrrrrrrrrrrrrrrre/li>PHProl-s="udih"detv> "udo-​whan>f% like trrrrrrrrrrrrrrrrrrrre/li>PHProl-s="udih fseBA like trrrrrrrrrrrrrrrrrrrre/li>PHProl-s="udihfseaas=BA like trrrrrrrrrrrrrrrrrrrre/li>PHProl-s="udih beaakBA like trrrrrrrrrrrrrrrrrrrre/li>PHProl-s="udihPHPinue"detv> PHPinuestivHPinueBA like trrrrrrrrrrrrrrrrrrrre/li>PHProl-s="udih swits=BA like trrrrrrrrrrrrrrrrrrrre/li>PHProl-s="udih"detv> "udean r>BA like trrrrrrrrrrrrrrrrrrrre/li> up up up PHProl-s="udih gotoBA like trrrrrrrrrrrrrrrrrrrre/li> ss="votes -1 PHPawhetntooier-contn c">equire&copyr="ti"det>Copyr="ti © 2001-2017 Tequire&my"det>Mysд.tet;equire&>PHPaci"det>CoHPaci;equire&sitds"det>Orefs д.tet sitds/votequire&min/ses"det>Min/se sitds/votequire&privacy"det>Privacy pot">y/vot v class="vfooier." f=/js/="h/modd="izr.js"> f=/js/="h/hogan-2.0.0.min.js"> f=/js/="h/er="ahead.min.js"> f=/js/="h/mousetrap.min.js"> f=/js/class=.js"> f=/js/ths on.js"> equl"> toTop-13 05:4javanes/wh:;"to usinl"> toTopHovs="geiving yim Ie dth="40" he="ti="40" alt="To Top-1 rc="/imsern/to-top@2x.png">ears e/bodys"idive s
up
-1
11311="href="#7307" class="name"> gabe at web11311="hVu60987"11 years ago
up
-1
49864"href="#7342" class="name">Anonymous49864"hVu60987"trong>11 years ago