I think this organisation will be one of the big players of the future in the PHP world. 我认为,这个组织将在未来PHP的世界大牌球星之一。”
达伦

在其他文件,包括要求的文件

通常你想要PHP代码创建单独的文件在不同的任务。 例如,你可能访问一个文件 database.php ,数据库处理用户和最终的主网页的index.php 另一个文件users.php。

在PHP文件可以包含在主要使用include或 require功能的页面。

这些功能都在下面的例子可以互换的,有一点不同的解释如下。 功能包括文件,因此,当PHP文件的评估与包括这似乎是一个文件,因此功能都彼此可见,不含任何面向对象的问题。

php file PHP文件数据库
php <?PHP
connect ( ) 功能连接()
{
connects to the database / /连接到数据库
}
?>

php file用户。PHP文件
php <?PHP
isUser ( $ username ) 功能isUser(用户名
{
checks if a username is an existing user . / /检查一个用户名,如果是现有的用户
}
?>

php file索引。PHP文件
php <?PHP
include the database file / / 包括数据库文件
' database . php ' ) ; (“数据库PHP。);
include the users file / / 包括用户文件
' users . php ' ) ; 包括(“用户PHP。');

login ( $ username , $ password ) 功能登录 用户名密码
{
This function was present in the database . php file / /这个函数是在数据库中。PHP文件
连接();
This function was present in the database . php file / /这个函数是在数据库中。PHP文件
isUser ( $ username ) ) (isUser(用户名))
{
Carry on logging in / /进行登录
}
}
?>
正如你可以看到上面包括功能包括在index.php文件中的文件,让index.php文件使用database.php和users.php功能。 当然,任何项目的变量,类或database.php和users.php功能等,可使用的功能。
' user2 . php ' ) ; 要求(“user2PHP。”);
这里的user2.php文件不存在,但使用需要的功能是因为它会抛出一个致命的错误文件要求的功能表明,这个文件需要user2.php的文件exectute。 但是, 如果包括使用它只会返回一个警告文件是不需要执行。 可能出现的另一个问题是,如果一个文件被包含在一个子文件是包含在父文件,它可能会导致一个错误,所以下面我们有发生这种情况的一个例子。
php file PHP文件数据库
php <?PHP
/ *
The rest of the database file *其余的数据库文件
/ *
?>

php file用户。PHP文件
php <?PHP
include the database file / / 包括数据库文件
' database . php ' ) ; (“数据库PHP。);
/ *
The rest of the users file *其余的用户文件
/ *
?>

php file索引。PHP文件
php <?PHP
include the users file / / 包括用户文件
' users . php ' ) ; 包括(“用户PHP。');
Its included again ! ! / /再次包含
' database . php ' ) ; (“数据库PHP。);

/ *
The rest of the index . php file *其余的指数。PHP文件
* /
?>
因为database.php文件已被列入了两次,一次在users.php文件中,一旦在index.php文件中,PHP会抛出一个错误。

确保文件只包括或要求,一旦

为了确保文件只包括或要求,一旦你需要使用和include_oncerequire_once这些文件将只包含一个文件,曾经多少次文件是在儿童档案的问题。这应该永远使用,如果有机会一个文件可以包含多次。例如...
php file PHP文件数据库
php <?PHP
/ *
The rest of the database file *其余的数据库文件
/ *
?>

php file用户。PHP文件
php <?PHP
include the database file / / 包括数据库文件
' database . php ' ) ; include_once(“PHP数据库。');
/ *
The rest of the users file *其余的用户文件
/ *
?>

php file索引。PHP文件
php <?PHP
include the users file / / 包括用户文件
' users . php ' ) ; include_once(“用户PHP。');
Its included again ! ! / /再次包含
' database . php ' ) ; include_once(“PHP数据库。');

/ *
The rest of the index . php file *其余的指数。PHP文件
* /
?>
当index.php文件是评估它不会抛出一个错误,因为database.php文件只包含一次users.php文件users.php是唯一一次在index.php文件中包含。 作为一个规则,它通常总是使用的和 include_once require_once 功能包括和需要的功能,而不是一个更好的主意。

最新的评论:1。 第1页共1页。 平均评分:
11:09am on Friday, July 15th, 2011 刘慧卿 日星期五上午十一点09分,7月15日,2011年
请保持thriowng这些职位时,他们帮助吨。

安全形象
作者由Dominic斯金纳
最后更新:2011年10月25日16点00分38秒