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 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程