"include_once"和"include_once"之间的区别是什么?和"require_once"在PHP中? [英] What is difference between "include_once" and "require_once" in PHP?

查看:129
本文介绍了"include_once"和"include_once"之间的区别是什么?和"require_once"在PHP中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP中"include_once"和"require_once"之间有什么区别?

What is difference between "include_once" and "require_once" in PHP?

include_once "connect_to_mysql.php";

require_once "connect_to_mysql.php";

两者之间有什么区别吗?

Is there any difference between these?

推荐答案

如果文件丢失,include将使脚本继续运行(带有警告).

Include will let the script keep running (with a warning) if the file is missing.

如果缺少,Require将会崩溃.

Require will crash if it's missing.

如果您对100%可选的内容使用包含,则包含将仍然有效,需求会爆炸.

If you're using include for things which are 100% optional, then include will still work and require will explode.

如果您包括一些您认为是可选的内容,但脚本的其他部分仍在使用它,那么您的脚本将在那里爆炸,您可能不知道为什么.

If you include something that you think is optional, but some other piece of your script uses it, way down the line, then your script will explode there, and you'll probably have no idea why.

这不是编写程序的好方法.

This is not a good way to write programs.

否则,两者之间没有区别.

Otherwise, there isn't a difference between the two.

在典型用法中,选择是否使用95%的时间includerequire都无关紧要.

In typical usage, it shouldn't matter whether you choose to use include or require, 95% of the time.

因此,您应该坚持使用require(或require_once),以在丢失所需文件时告诉您.

As such, you should stick to require (or require_once), to tell you when you're missing a file you need.

由于缺少文件而导致的错误真的很难找到.

The bugs that come from including files inside of included files, inside of included files, when one include up top is missing, are really hard to track down.

某些人更喜欢include,因为他们想使用该功能".
道格拉斯·克罗克福德(Douglas Crockford)是JavaScript/Java/C ++专家,而不是PHP,但他建议您应该避免使用看起来像bug的功能或与bug难以区分的副作用.

Some people prefer include, because they want to use that "feature".
Douglas Crockford is a JavaScript/Java/C++ guy, rather than PHP, but he suggests that features that look like bugs, or side effects which are indistinguishable from bugs, should be avoided for your sanity.

提醒您,如果您的整个项目完全基于类(或功能),并且完全模块化,那么除了在应用程序初始化时在配置对象上重置值(例如,包括已覆盖require d的对象上的admin-override选项或dev-debugging选项.

Mind you, if your entire project is completely class-based (or functional), and entirely modular, then you shouldn't have much use for include, aside from, say resetting values on a configuration-object at application initialization (including admin-override options or dev-debugging options) on an object that was already required.

您可能会猜到,还有其他方法可以实现.

As you might guess, there are other ways of doing this.

这些只是建议,但这些建议来自比我聪明的人,他们拥有数十种语言的数十年经验.

And these are just suggestions, but suggestions that come from people who are smarter than I am, with decades of experience in dozens of languages.

这篇关于"include_once"和"include_once"之间的区别是什么?和"require_once"在PHP中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆