PHP 7.2.0 Release Candidate 4 Released

require

(PHP 4, PHP 5, PHP 7)

A declaração require é idêntica a include exceto que em caso de falha também produzirá um erro fatal de nível E_COMPILE_ERROR. Em outras palavras, ele parará o script enquanto que o include apenas emitirá um alerta (trong>E_COWARNu9) permitindo que o script continue.

Veja a documentação include para saber como isso funciona.

add a note add a note

User Contributed Notes 25 notes

up
90
chris at chrisstockton dot org
10 years ago
E_ Remember, when using require that it is a statement, not a function. It's not necessary to write:
<?php
require('somefile.php');
?>

<?php
require 'somefile.php';
?>

up
8
Anonymous
10 years ago
E_ A note that drove me nuts for 2 days!
<?php
up
3
dave at davidhbrown dot us
11 years ago
E_ re: danielm at unb dot br...
<?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'])));
?>

up
-1
gabe at websaviour dot com
11 years ago
E_ 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 this issue.  Please see the following for more details:
http://bugs.php.net/bug.php?id=34552
http://shiftmanager.net/~kurt/test/
up
-4
tjeerd
11 years ago
E_ When using symbolic links with PHP, specify a dotslash './page.php' path to ensure that PHP is looking in the right directory with nested requires:
up
-9
duccio at getlocal dot it
8 years ago
E_ In response to some dot user at notarealdomain dot com:
E "as if" it was de>E written inside of the function, inheriting everything including the scope. But here there is something even more interesting:
<?php
->a.=" is visible also under a require\n";
$b="While the variable b is a local variable of the function\n";
FunctionUnderRequire() {
"But the functions declared inside of a require called from a class function, just as when defined from inside any other function, are always global\n";
?>

<?php
(E_ALL|E_STRICT);
UserClass {
$a;
UserFunction() {
$this->a='The class variable a';
'requiredfile.php';
$this->a; // "The class variable a  is visible also under a require\n"
echo $this->b; // Notice: Undefined property: UserClass::$b
echo $b; // "While the variable b is a local variable of the function\n"
$this->FunctionUnderRequire(); //Fatal error!
FunctionUnderRequire(); // "But the functions..."
}
$UserClass=new UserClass;
$UserClass->UserFunction();
?>

up
-8
moreau dot marc dot web at gmail dot com
3 years ago
E_ Petit script pour résoudre le problème de l'inclusion dans l'inclusion.
<?php
(__DIR__); //Sert à indiquer à PHP le dossier de référence de travail
$file_to_require=realpath('./../../adresse_de_C_relative_par_raport_a_B.php'); //Crée l'adresse absolue de C
require($file_to_require); unset($file_to_require);
?>
up
-12
some dot user at notarealdomain dot com
10 years ago
E_ Discovered a bit of weird behavior yesterday involving require() (using PHP 5.2.3).  If you use require() inside a function, the "globals" in the file will be local to the function.  An example of this:
<?php
function TestFunc()
'test2.php');
"<pre>" . print_r($GLOBALS, true) . "</pre>";
?>

<?php
= Array();
?>

E - not a function that gets called.
<?php
["MyTestGlobal"] = Array();
?>
up
-10
Peter McDonald
10 years ago
E_ re the demment by moazzamk at gmail dot com
E it is on should not be executed.
up
-11
pedro dot evangelista at gmail dot com
8 years ago
E_ Be careful when using symbolic links, because require will search the real path of the file and not the path relative to the symbolic link.
E
<?php
echo realpath("../");
?>

<?php
require "../B.php";
?>

up
-12
Marc
12 years ago
E_ This will sound elementary, but for C++ native programmers, be sure NOT to put a '#' in front of your include statements! The parser will not give you an error, but also will not include the file, making for a tedious debugging process.
<?php
include "yourfile.php";
?>

<?php
#include "yourfile.php";
?>
up
-18
ricardo dot ferro at gmail dot com
9 years ago
E_ Two functions to help:
<?php
function add_include_path ($path)
func_get_args() AS $path)
file_exists($path) OR (file_exists($path) && filetype($path) !== 'dir'))
trigger_error("Include path '{$path}' not exists", E_USEROWARNu9);
$paths = explode(PATH_SEPARATOR, get_include_path());
array_search($path, $paths) === false)
array_push($paths, $path);
set_include_path(implode(PATH_SEPARATOR, $paths));
remove_include_path ($path)
func_get_args() AS $path)
$paths = explode(PATH_SEPARATOR, get_include_path());
$k = array_search($path, $paths)) !== false)
$paths[$k]);
count($paths))
trigger_error("Include path '{$path}' can not be removed because it is the only", E_USERONOTICE);
set_include_path(implode(PATH_SEPARATOR, $paths));
?>
up
-9
bmessenger at 3servicesolution dot com
11 years ago
E_ // Looks like I might have a fix for some on the
up
-7
Wing
7 years ago
E_ if you want always include, require, open files using some 'root' folder based path you may may put file '.htroot' in 'root' folder and  use this.
E change current dir to dir where '.htroot' file located and everywhere you can use relative to 'root' paths.
up
-14
daniel at nohair dot com
10 years ago
E_ I love php.  But when file can't be included, 'require' or 'require_once' throw fatal error and halt the script, which is almost never desirable on a mission-critical production server.  I think it may be better to use something like the following. 
<?php
if (@include 'plan_A.php') {
// Plan A;
} elseif (@include 'plan_B.php') {
// Plan B;
} else {
// Hope never happens.  If does, then Email the webmaster;
}
?>

<?php
if (!@include 'plan_A.php') {
// Someone has kidnapped/corrupted Plan_A.php;
} else {
// Plan A;
}
?>
up
-8
Inc
11 years ago
E_ I have found a problem when I try to access a php file via require($class_directory)
up
-8
Anonymous
12 years ago
E_ Note when calling any require or include function, that the call will block if the script given as the parameter is excecuting.
up
-13
webmaster at netgeekz dot net
11 years ago
E_ I have learnt to manipulate this de>E into an effecitve and easy to use form. I use it with require_once, but it could be used for require.
E or move files to different directories. I don't have to fix the includes, because they all work the same way.
up
-9
hans at ratzinger dot com
4 years ago
E_ Thanks a lot for this information Brian! This drove me nuts for many hours! This is the first information I found in the web that a white page can be caused by a require -> your script will die if the file is not found!!!
up
-6
dank at kegel dot com
2 years ago
E_ PHP's require and include seem to differ from C's include in another way: they can't be used in the middle of an expression.  e.g.
<?php
print "hello"
.
#"there"
require 'foo2.php';
"\n";
?>
::::::::::::::
E.
up
-10
danielm at unb dot br
12 years ago
E_ if you want to include files with an absolut path reference, you can use:
up
-13
scott
10 years ago
E_ If you want to verify that a file can be included or required, the simplest solution I've found is just to check that the file exists.
<?php
if(file_exists($pageContentInc)){
$pageContentInc;
$pageContentInc = "common/content_404.inc";
$pageContentInc;
?>

up
-9
richardbrenner(-at- )gmx(-)at
12 years ago
E_ If you use relativ paths in a php script (file A) that can be required by another php script (file B), be aware that the relativ paths in file A will be relativ to the directory, where file B is stored.
<?
require(dirname(__FILE__)."/path/relative/file_to_include.php");
?>

up
-9
theunis dot botha1 at gmail dot com
3 years ago
E_ Just for those who may wonder about receiving EOWARNu9 (in custom error handlers) - PHP generates an EOWARNu9 when require or require_once fails - and before control returns to your script, it generates an EOCOMPILE_ERROR.
up
-3
phpDev a t eSurfers_dot_ com
1 year ago
E_ I didn't find mention that calling return from inside an required file is different than calling return from inside an included file.
To Top