当前位置:网站首页>PHP require/include 区别
PHP require/include 区别
2022-06-30 20:05:00 【烟草的香味.】
前言
在PHP中, 载入文件可以选择使用require, 也可以使用include, 那么那他们有什么区别呢? 看了网上的一些文章, 说他们使用场景不同, require一般在文件开头引入文件, include一般在函数中动态引入文件.
但是我觉得并不是这么简单, require是作为语言结构(关键字)出现的, 关键字这玩意对于语言设计者来说一般都是越少越好, 关键字少了语言才简单嘛. 于是我决定深入到源码级别来康康他们的区别.
探究
通过查看, require/require_once/include/include_once这几个方法翻译后是同一个c函数ZEND_INCLUDE_OR_EVAL_SPEC_CONST_HANDLER.
如何拿到这个c函数可看这篇文章: https://hujingnb.com/archives/836
查看方法:

include/require
include/require方法的处理方式相同. 我们先看他们的区别, 再去看_once方法的区别.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2bjPcQLE-1656157366388)(https://oss-blog.cdn.hujingnb.com/img/202206251901506.png)]
通过查找, include与require的唯一区别, 就是当文件不存在的时候处理方式不同.

再次查找, 对于二者的处理方式如图所示(这个报错文案确实是文件不存在时候的文案). 对于调用的两个方法作用如下:
php_error_docref: 抛出php级别错误. 因为这里抛出的warning级别, 故不会令脚本停止.zend_throw_error: 抛出编译期异常, 会终止脚本执行.
因此, 通过比较看到, require和include的唯一区别, 就是当文件不存在的时候, require会终止脚本执行, include仅抛出warning错误.
这里在zend_throw_error方法中看到了下面注释, 其中提到当前无法将编译期错误转换为exception. 可能未来会支持??

include_once/require_once
通过查看源码, include_once/require_once的区别, 与include/require一样, 都是在对待异常的处理上不同, 其他完全一样

结论
因此, include和require的唯一区别, 就是当文件不存在的时候, require会终止进程, 而include不会.
啊, 这, 整两个如此相似的关键字真的好么, 如果说, 将require去掉, 然后通过include返回值来判断文件是否存在, 由调用者来判断是否要终止进程, 这样是否会更合理一些呢? 亦或者PHP的设计师们是出于其他方面的考虑?
边栏推荐
- 开会,OneMeeting,OK!
- Web host iptables firewall security script
- PM reports work like this, and the boss is willing to give you a raise
- Halcon knowledge: check the measurement objects [1]
- HeartBeat基于CRM使用NFS对MySQL高可用
- Common questions and answering skills of project manager interview
- Tensorflow2.4 implementation of repvgg
- Jerry's determination of detection sensitivity level [chapter]
- Jerry's question about long press boot detection [chapter]
- How unity pulls one of multiple components
猜你喜欢

Lambda expression principle analysis and learning (June 23, 2022)

杰理之触摸按键识别流程【篇】

CADD课程学习(1)-- 药物设计基础知识

Transport layer uses sliding window to realize flow control

消灭Bug,开发者不可不知的几款Bug探索测试神器。
![Halcon knowledge: check the measurement objects [1]](/img/0a/3a12e281fcb201d8d11b25dac4145a.png)
Halcon knowledge: check the measurement objects [1]

Tensorflow2.4实现RepVGG

左值引用和右值引用

maya房子建模

Jenkins can't pull the latest jar package
随机推荐
Why should offline stores do new retail?
Maya house modeling
CADD course learning (2) -- target crystal structure information
How unity pulls one of multiple components
Jerry's long press reset [chapter]
Lambda 表达式原理分析学习(2022.06.23)
Solve the problems of Devops landing in complex environment with various tools with full stack and full function solutions
Is it safe to open an account for online stock trading!?
Black apple server system installation tutorial, black apple installation tutorial, teach you how to install black apple in detail [easy to understand]
北京大学ACM Problems 1001:Exponentiation
【ICLR 2021】半监督目标检测:Unbiased Teacher For Semi-Supervised Object Detection
Informatics Olympiad 1362: family problems
mysql登录出现1045错误修改方法[通俗易懂]
[ICLR 2021] semi supervised object detection: unbiased teacher for semi supervised object detection
Primary school, session 3 - afternoon: Web_ xxe
pytorch实现FLOPs和Params的计算
The Commission is so high that everyone can participate in the new programmer's partner plan
Document contains & conditional competition
计网 | 【五 传输层、六 应用层】知识点及例题
Description of the latest RTSP address rules for Hikvision camera, NVR, streaming media server, playback and streaming [easy to understand]