Home > Backend Development > PHP Tutorial > php里的include和require

php里的include和require

WBOY
Release: 2016-06-23 14:32:20
Original
841 people have browsed it

 今天偶然看到的,摘录之
在PHP变成中,include()与require()的功能相同,但在用法上却有一些不同,include()是有条件包含函数,而require()则是无条件包含函数。例如在下面的一个例子中,如果变量$somgthing为真,则将包含文件somefile:

if($something){
 include("somefile");
}


  但不管$something取何值,下面的代码将把文件somefile包含进文件里:

if($something){
 require("somefile");
}


  下面的这个有趣的例子充分说明了这两个函数之间的不同。

$i = 1;
while ($i  require("somefile.$i");
 $i++;
}


  在这段代码中,每一次循环的时候,程序都将把同一个文件包含进去。很显然这不是程序员的初衷,从代码中我们可以看出这段代码希望在每次循环时,将不同的文件包含进来。如果要完成这个功能,必须求助函数include():

$i = 1;
while ($i  include("somefile.$i");
 $i++;
}

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template