Home >Backend Development >PHP Tutorial >PHP中include跟require的区别

PHP中include跟require的区别

WBOY
WBOYOriginal
2016-06-13 12:23:42846browse

PHP中include和require的区别

include和require的区别,其实两者没有太大的区别,如果要包含的文件不存在,include提示notice,然后继续执行下面的语句,require提示致命错误并且退出。

根据测试,win32平台下它们都是先包含后执行,所以被包含文件里最好不要再有include或require语句,这样会造成目录混乱。

或许*nux下情况不同,暂时还没测试。如果一个文件不想被包含多次可以使用include_once或require_once## 读取,写入文档数据:

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> r(<span style="color: #800080;">$file_name</span><span style="color: #000000;">) {</span><span style="color: #008080;"> 2</span> <span style="color: #800080;">$filenum</span>=@<span style="color: #008080;">fopen</span>(<span style="color: #800080;">$file_name</span>,"r"<span style="color: #000000;">);</span><span style="color: #008080;"> 3</span> @<span style="color: #008080;">flock</span>(<span style="color: #800080;">$filenum</span>,<span style="color: #000000;">LOCK_SH);</span><span style="color: #008080;"> 4</span> <span style="color: #800080;">$file_data</span>=@<span style="color: #008080;">fread</span>(<span style="color: #800080;">$filenum</span>,<span style="color: #008080;">filesize</span>(<span style="color: #800080;">$file_name</span><span style="color: #000000;">));</span><span style="color: #008080;"> 5</span> @<span style="color: #008080;">fclose</span>(<span style="color: #800080;">$filenum</span><span style="color: #000000;">);</span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$file_data</span><span style="color: #000000;">;</span><span style="color: #008080;"> 7</span> <span style="color: #000000;">}</span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">function</span> w(<span style="color: #800080;">$file_name</span>,<span style="color: #800080;">$data</span>,<span style="color: #800080;">$method</span>="w"<span style="color: #000000;">){</span><span style="color: #008080;"> 9</span> <span style="color: #800080;">$filenum</span>=@<span style="color: #008080;">fopen</span>(<span style="color: #800080;">$file_name</span>,<span style="color: #800080;">$method</span><span style="color: #000000;">);</span><span style="color: #008080;">10</span> <span style="color: #008080;">flock</span>(<span style="color: #800080;">$filenum</span>,<span style="color: #000000;">LOCK_EX);</span><span style="color: #008080;">11</span> <span style="color: #800080;">$file_data</span>=<span style="color: #008080;">fwrite</span>(<span style="color: #800080;">$filenum</span>,<span style="color: #800080;">$data</span><span style="color: #000000;">);</span><span style="color: #008080;">12</span> <span style="color: #008080;">fclose</span>(<span style="color: #800080;">$filenum</span><span style="color: #000000;">);</span><span style="color: #008080;">13</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$file_data</span><span style="color: #000000;">;</span><span style="color: #008080;">14</span> }

 



 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn