包含/内部条件的PHP行为 [英] PHP behavior of include/require inside conditional
问题描述
如果我在评估为 false的条件中放置
,PHP解释器是否会完全跳过包含文件,还是会加载它以防万一? include
或 require
语句
If I place an include
or require
statement inside a conditional that evaluates to false
, will the PHP interpreter skip the include file altogether, or will it load it just in case?
一个例子是:
if ($some_user_var) {
require 'this.php';
} else {
//do stuff
}
I在某处读取 require
将始终包含在解释器中,无论条件如何,但 include
都不会。如果是这种情况,只需从需要
切换到 include
就可能意味着由于I / O减少而导致免费加速解析开销。
I read somewhere that require
will always be included by the interpreter regardless of the conditional, but include
will not. If that's the case, just switching from require
to include
could mean a free speedup due to the reduced I/O and parsing overhead.
如果我正在运行像eAccelerator这样的预处理器,它可能会有所不同,但我们假设我不这样做。
It probably makes a difference if I'm running a preprocessor like eAccelerator, but let's assume I don't.
推荐答案
只有在条件为真时才会包含它。我不知道你在哪里阅读,但他们错了。
It will only be included if the condition is true. I don't know where you read otherwise, but they're wrong.
之间的唯一区别包括
和 require
是 include
如果失败则会发出警告,而 require
将抛出致命错误。
The only difference between include
and require
is that include
will throw a warning if it fails, whereas require
will throw a fatal error.
要确认这一点,请参阅 的PHP手册页需要
。
To confirm this, see the PHP manual page for require
.
(ps - 如果你正在做条件包括,取决于什么是reaon,你可以考虑使用 include_once()
或 require_once()
代替)
(ps - if you're doing conditional includes, depending on what the reaon is, you may consider using include_once()
or require_once()
instead)
这篇关于包含/内部条件的PHP行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!