require() and include() have many similarities and some differences. It's important to understand their differences, otherwise it's easy to make mistakes.
require() statement is used to specify the file instead of the statement itself, just like the include() statement in C language. If the URL fopen wrappers in the php configuration file php.ini is turned on (it is turned on by default), you can use the URL to specify the location of the file to achieve remote file calling.
One thing is to pay special attention when using require() and include() statements. That is, in the included file, the processor interprets the content according to the HTML mode, and returns to the PHP mode after processing the included content. So if you need to use PHP syntax in the included file, you must use the correct PHP start and end tags to include these statements.
require() and include() are just a language feature in PHP, not a function. They are different from functions in many ways.
For example: the file included in require() cannot contain control structures, and statements such as return cannot be used. Using the return statement in a file included with require() will generate a processing error.
Unlike the include() statement, the require() statement will unconditionally read the contents of the files it contains, regardless of whether these statements are executed. So if you want to include different files according to different conditions, you must use the include() statement. Of course, if the statement at the location of require() is not executed, the statements in the file contained by require() will not be executed either.
require() cannot include different files based on different conditions in the loop body. The require() statement will only call the content of the file it contains when it is executed for the first time to replace the statement itself. When it is executed again, only the statement included in the first time will be executed. But the include() statement can include different files in the loop body.
Variables in the require() statement inherit the variable scope of the location where the require() statement is located. All variables accessible at the location of the require() statement are accessible in the file included in the require() statement. If the require() statement is located inside a function, then the statements in the included file are equivalent to being defined inside the function.
Therequire() statement will read the file referenced by require before the PHP program is executed, so require is usually placed at the beginning of the program. Therefore, special attention should be paid to the fact that the require statement is a bit strong. Regardless of whether the program really needs the referenced files, as long as you use the require statement, it will include them! Even if you use this function to include in a conditional control statement, even if the condition is not true, the referenced file will be included! Zombies are formed. These zombies will not have any visible effect during operation, but it will obviously increase the burden, so pay special attention to this! If an inclusion error occurs using the require statement, the program will output an error message and stop running! !
If the require() statement includes a remote file by declaring the URL of the file, and the remote server interprets the file according to the PHP code, the content contained in the local PHP file is the result of processing on the remote server. For example:
/* 这个例子假设some_server服务器可以解释.php文件,而不对.txt文件进行解释。在远程文件中 需要变量$varfirst和$varsecond */ /*不能正确执行,远程服务器不处理.txt文件*/ require("http://some_server/file.txt?varfirst=1&varsecond=2"); /*不正确,这样只能在本地机上寻找file.php文件*/ require("file.php?varfirst=1&varsecond=2"); /*正确的语句*/ require("http://some_server/file.php?varfirst=1&varsecond=2"); $varfirst=1; $varsecond=2; require("file.txt"); /*正确的语句*/ require("file.php"); /*正确的语句*/
Originally in php3.0, files included in require() can use the return statement, but the condition is that the return statement cannot appear inside {}, but must appear in the global scope of the included file. This function of require() has been canceled in php4.0, but it can still be implemented using include().
The include() statement and the require() statement have many similarities. Except for those parts that are not explicitly stated in the above require() statement and cannot be applied to include(), the functions of the require() statement are fully applicable to the include() statement. The following describes the functions and features of the include() statement that are not available in the require() statement.
The include statement will only read in the files to be included when it is executed. To facilitate error handling, use the include statement. If an include error occurs, the program will skip the include statement. Although the error message will be displayed, the program will continue to execute!
The PHP processor will reprocess it every time it encounters an include() statement, so you can use include() in conditional control statements and loop statements to include different files according to different situations.
<?php $files=array('first.php','second.php','third.php'); for($i=0;$i<count($files);$i++) { include $files[$i]; } ?>
In the files included in the include() statement in php3.0 and php4.0, you can use the return statement to return a value and stop executing the content under the included file. But php3.0 and php4.0 handle such situations differently. In php3.0 the return statement cannot be contained within {} unless it is in a function, because then it represents the return value of the function rather than the return value of the file. In php4.0, there is no such restriction. Users can even return a number in the file, just like the return value of a function. Such statements usually report errors in php3.0. Here are some examples:
Assume that the included file is test.inc and the main file main.php is located in a directory. The content of test.inc is as follows:
<?php echo "Before the return<br>n"; if(1) { return 27; } echo "After the return<br>n"; ?>
Suppose the following statement is included in the main.php file:
<?php $retval=include('test.inc'); echo "File returned:'$retval'<br>n"; ?>
php3.0解释器会在第二行报告错误,而不能得到include()语句的返回值。但在php4.0中会得到下面的结果:
Before the return File returned: '27'
下边假设main.php改为:
<?php include('test.inc'); echo "Back in main.html<br>n"; ?>
在php4.0中的输出结果是:
Before the return Back in main.html
在php5.0中的输出结果也是:
Before the return Back in main.html
在php3.0中的输出结果是:
Before the return 27Back in main.html
Parse error:parse error in /apache/htdocs/phptest/main.html on line 5
出现上面的错误是因为return语句位于{}内部而且不是一个函数内部。如果把{}去掉,使它位于test.inc的最外层,输出结果是:
Before the return 27Back in main.html
之所以出现27,是因为在php3.0中不支持include()返回。
require_once()和include_once()语句分别对应于require()和include()语句。require_once()和include_once()语句主要用于需要包含多个文件时,可以有效地避免把同一段代码包含进去而出现函数或变量重复定义的错误。例如:如果创建两个文件util.inc和fool.inc,程序代码分别为:
util.inc:
<?php define(PHPVERSION,floor(phpversion())); echo "GLOBALS ARE NICE<br>n"; function goodTea() { return "Olong tea tasts good!"; } ?>
和fool.inc:
<?php require ("util.inc"); function showVar($var) { if(PHPVERSION==4) { print_r($var); } else { var_dump($var); } } ?>
然后在error_require.php中包含这两个文件:
<?php require("fool.inc"); require("util.inc");//此句会产生一个错误 $foo=array("1",array("complex","quaternion")); echo "this is requiring util.inc again which is also<br>n"; echo "required in fool.incn"; echo "Running goodTea:".goodTea()."<br>n"; echo "Printing foo:<br>n"; showVar($foo); ?>
当运行error_require.php时,输出结果如下:
GLOBALS ARE NICE GLOBALS ARE NICE Fatal error:Cannot redeclare goodTea() in util.inc on line 4
如果使用require_once()语句来代替 require()语句,就不会出现上面的错误。我们把error_require.php和fool.inc中的require()语句改为require_once()语句并重命名为error_require_once.php,这是显示结果如下:
GLOBALS ARE NICE this is requiring util.inc again which is also required in fool.inc Running goodTea:Olong tea tastes good! Printing foo: Array([0] => 1 [1] => Array ([0] => complex [1] = quaternion))
include_once()语句的语法和include()语句类似,主要区别也是避免多次包含一个文件而引起函数或变量的重复定义。
require_once语句有一个引用链,它可以保证文件加入你的程序仅仅只有一次,而且会避开变量值和函数名之间的冲突。
和require_once语句一样,include_once语句把include的功能扩展了。在程序执行期间,将指定的文件包含进来,如果从文件引用进来的程序先前已经包含过的时候,include_once()就不会把它再包含进来。也就是仅仅可以引用同一个文件一次!
include_once() 语句在脚本执行期间包含并运行指定文件。此行为和 include() 语句类似,唯一区别是如果该文件中的代码已经被包含了,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。
include_once() 应该用于在脚本执行期间同一个文件有可能被包含超过一次的情况下,想确保它只被包含一次以避免函数重定义,变量重新赋值等问题。
使用 require_once() 和 include_once() 的更多例子见最新的 PHP 源程序发行包中的 PEAR 代码。
返回值和 include() 相同。如果文件已被包含,本函数返回 TRUE。
注: include_once() 是 PHP 4.0.1pl2 中新加入的。
注: 要注意 include_once() 和 require_once() 在大小写不敏感的操作系统中(例如 Windows)的行为可能不是所期望的。
如果一个文件不想被包含多次可以使用include_once或require_once## 读取,可以写入文档数据。
<?php function r($file_name) { $filenum=@fopen($file_name,"r"); @flock($filenum,LOCK_SH); $file_data=@fread($filenum,filesize($file_name)); @fclose($filenum); return $file_data; } function w($file_name,$data,$method="w") { $filenum=@fopen($file_name,$method); flock($filenum,LOCK_EX); $file_data=fwrite($filenum,$data); fclose($filenum); return $file_data; } ?>