PHP behavior of include/require inside conditional(条件内包含/要求的 PHP 行为)
问题描述
如果我将 include
或 require
语句放在计算结果为 false
的条件中,PHP 解释器会完全跳过包含文件吗?还是会加载它以防万一?
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
}
我在某处读到 require
将始终被解释器包含,而不管条件如何,但 include
不会.如果是这种情况,仅仅从 require
切换到 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.
include
和 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.
要确认这一点,请参阅 require
的 PHP 手册页.
To confirm this, see the PHP manual page for require
.
(ps - 如果您正在执行条件包含,取决于原因,您可以考虑使用 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 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:条件内包含/要求的 PHP 行为
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01