dqfkd82886 2010-09-02 10:33
浏览 36
已采纳

PHP require和include有什么区别?

I know the basic usage of PHP require, require once, include and include once. But I am confused about when I should use them.

Example: I have 3 files, eg: settings.php, database.php, and index.php.

In database.php file, i wrote:

require_once 'settings.php';

and then in index.php, i wrote:

require_once 'settings.php';
require_once 'database.php';

so I load settings.php two times, is this okay? Any tips using these functions?

  • 写回答

7条回答 默认 最新

  • douzhi2988 2010-09-02 10:37
    关注
    • include includes a file and throws a warning if the file was not found.

    • require includes a file and throws a fatal error if the file was not found.

    • include_once and require_once do the same thing, but only if the file was not already loaded.

    However, the need for one of the _once functions is usually a sign of bad design. You should build your scripts in a way that clearly defines what gets included where.

    Choose one place for settings.php to get included - probably index.php. There should be no need to additionally include it in database.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖