include,include_once,require或require_once? [英] include, include_once, require or require_once?
问题描述
我有PHP文件,我已经定义了服务器访问变量以及 mysql_connect
和 mysql_select_db
,如这个函数经常在后端的每个页面中使用,而我正在使用 include()
这对我现在非常有效,你会建议哪种方法或功能,我会如果我使用 include()
或者使用它是否安全,我想知道是否有任何缺陷?
I have PHP file where I have defined the server access variables as well as the mysql_connect
and mysql_select_db
, as this functions are regularly used in almost every page in backend, while I am using include()
which is perfectly working for me now, which method or function would you suggest and I would like to know if there is any flaw if I use include()
or is it safe to use it?
编辑:请记住,我也将使用 $ _ SESSION
变量。
Edit : Keeping in mind I'll be using $_SESSION
variable too.
推荐答案
两者之间的唯一区别是需要
及其姐妹 require_once
如果找不到文件则抛出致命错误,而包括
和 include_once
仅显示警告并继续加载页面的其余部分。如果您不希望PHP在没有数据库信息的情况下尝试加载页面的其余部分(我假设),则使用 require_once
。您不需要多次包含该文件,因此无需使用常规的 require
函数。
The only difference between the two is that require
and its sister require_once
throw a fatal error if the file is not found, whereas include
and include_once
only show a warning and continue to load the rest of the page. If you don't want PHP to attempt to load the rest of your page without the database info (which I would assume), then use require_once
. You don't need to include the file more than once, so there is no need to use the regular require
function.
这篇关于include,include_once,require或require_once?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!