花蜻宽 发表于 2017-3-22 12:01:10

PHP中include和require

转载自:http://blog.sina.com.cn/s/blog_5d2673da0100cp6o.html

?





<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">require()和include()有许多相似之处,也有些不同。理解它们的不同点非常重要,否则很容易犯错误。</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">????如果require()语句通过声明文件的URL来包含远程文件,而且远程服务器按照php代码来解释该文件的话,本地php文件中所包含的内容是在远程服务器上处理以后的结果。例如:<br>???<br><br>require("</span><a style="text-decoration: none; color: #599100;" href="http://some_server/file.txt?varfirst=1&amp;varsecond=2"><span style="">http://some_server/file.txt?varfirst=1&amp;varsecond=2</span></a><span style="">");</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style=""><br>require("</span><a style="text-decoration: none; color: #599100;" href="http://some_server/file.php?varfirst=1&amp;varsecond=2"><span style="">http://some_server/file.php?varfirst=1&amp;varsecond=2</span></a><span style="">");</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">2.include()语句<br>????include()语句和require()语句有许多相同的地方。凡是在上边require()语句中没有明确说明不能适用于include() 的部分外,require()语句的功能完全适用于include()语句。下边介绍require()语句所没有的include()语句的功能和特点。<br>????include语句只有在被执行时才会读入要包含的文件。在错误处理方便,使用include语句,如果发生包含错误,程序将跳过include语句,虽然会显示错误信息但是程序还是会继续执行!<br>???php处理器会在每次遇到include()语句时,对它进行重新处理,所以可以根据不同情况的,在条件控制语句和循环语句中使用include()来包含不同的文件。<br>???例如:<br><?php<br>$files=array('first.php','second.php','third.php');<br>???????for($i=0;$i<count($files);$i++)<br>???????{<br>???????include $files[$i];<br>}<br>?><br>???在php3.0和php4.0中include()语句所包含的文件中都可以使用return语句来返回一个值,并停止执行被包含文件下面的内容。但 php3.0和php4.0在处理这样的情况时有所不同。在php3.0中return语句不能包含在{}内,除非它在一个函数中,因为这时它表示函数的返回值而不是文件的返回值。而在php4.0中就没有了这样的限制,用户甚至可以在文件中返回一个数字,就象函数的返回值一样。这样的语句在</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">假设在main.php文件中包含下面的语句:<br><?php<br>$retval=include('test.inc');<br>echo "File returned:'$retval'<br>\n";<br>?><br>????php3.0解释器会在第二行报告错误,而不能得到include()语句的返回值。但在php4.0中会得到下面的结果:<br>Before the return<br>File returned: '27'<br>下边假设main.php改为:<br><?php<br>include('test.inc');<br>echo "Back in main.html<br>\n";<br>?><br>在php4.0中的输出结果是:<br>Before the return<br>Back in main.html</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">在php3.0中的输出结果是:<br>Before the return<br>27Back in main.html<br><br>Parse error:parse error in /apache/htdocs/phptest/main.html on line 5</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">3.require_once()和include_once()语句<br>require_once()和include_once()语句分别对应于require()和include()语句。require_once() 和include_once()语句主要用于需要包含多个文件时,可以有效地避免把同一段代码包含进去而出现函数或变量重复定义的错误。例如:如果创建两个文件util.inc和fool.inc,程序代码分别为:<br>util.inc:<br><?php<br>???define(PHPVERSION,floor(phpversion()));<br>???echo "GLOBALS ARE NICE<br>\n";<br>???function goodTea()<br>???{<br>??????????return "Olong tea tasts good!";<br>???}<br>?><br>和fool.inc:<br><?php<br>???require ("util.inc");<br>???function showVar($var)<br>???{<br>??????????if(PHPVERSION==4)<br>??????????{<br>????????????print_r($var);<br>??????????}<br>??????????else<br>??????????{<br>????????????var_dump($var);<br>??????????}<br>???}<br>?><br>然后在error_require.php中包含这两个文件:<br><?php<br>require("fool.inc");<br>require("util.inc");//此句会产生一个错误<br>$foo=array("1",array("complex","quaternion"));<br>???????echo "this is requiring util.inc again which is also<br>\n";<br>echo "required in fool.inc\n";<br>echo "Running goodTea:".goodTea()."<br>\n";<br>echo "Printing foo:<br>\n";<br>showVar($foo);<br>?><br>????当运行error_require.php时,输出结果如下:<br>????GLOBALS ARE NICE<br>????GLOBALS ARE NICE</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">include_once()语句的语法和include()语句类似,主要区别也是避免多次包含一个文件而引起函数或变量的重复定义。</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">和require_once语句一样,include_once语句把include的功能扩展了。在程序执行期间,将指定的文件包含进来,如果从文件引用进来的程序先前已经包含过的时候,include_once()就不会把它再包含进来。也就是仅仅可以引用同一个文件一次!</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">include_once() 应该用于在脚本执行期间同一个文件有可能被包含超过一次的情况下,想确保它只被包含一次以避免函数重定义,变量重新赋值等问题。</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">返回值和 include() 相同。如果文件已被包含,本函数返回 TRUE。</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">注: 要注意 include_once() 和 require_once() 在大小写不敏感的操作系统中(例如 Windows)的行为</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style=""><?php<br>include_once("a.php"); // this will include a.php<br>include_once("A.php"); // this will include a.php again on Windows! (PHP 4 only)<br>?></span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">如果要包含的文件不存在,include提示notice,然后继续执行下面的语句,require提示致命错误并且退出。</span>

<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;"><span style="">  如果一个文件不想被包含多次可以使用include_once或require_once## 读取,可以写入文档数据。<br><?php<br>function r($file_name) {<br> </span><a style="text-decoration: none; color: #599100;" href="mailto:%24filenum=@fopen(%24file_name,%22r"><span style="">$filenum=@fopen($file_name,"r</span></a><span style="">");<br> @flock($filenum,LOCK_SH);<br> </span><a style="text-decoration: none; color: #599100;" href="mailto:%24file_data=@fread(%24filenum,filesize(%24file_name"><span style="">$file_data=@fread($filenum,filesize($file_name</span></a><span style="">));<br> @fclose($filenum);<br> return $file_data;<br>}<br>function w($file_name,$data,$method="w"){<br> </span><a style="text-decoration: none; color: #599100;" href="mailto:%24filenum=@fopen(%24file_name,%24method"><span style="">$filenum=@fopen($file_name,$method</span></a><span style="">);<br> flock($filenum,LOCK_EX);<br> $file_data=fwrite($filenum,$data);<br> fclose($filenum);<br> return $file_data;<br>}<br>?></span>


<p style="margin-bottom: 5px; line-height: 21px; color: #323e32; font-family: simsun; font-size: 14px; text-align: left; background-color: #dceccc; padding: 0px;">很详细留下有需要的话再来看.
页: [1]
查看完整版本: PHP中include和require