当前位置:网站首页>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的设计师们是出于其他方面的考虑?
边栏推荐
- 建立自己的网站(20)
- 25: Chapter 3: developing pass service: 8: [registration / login] interface: receiving and verifying "mobile number and verification code" parameters; (it is important to know the application scenario
- Web host iptables firewall security script
- CADD课程学习(1)-- 药物设计基础知识
- 北京大学ACM Problems 1005:I Think I Need a Houseboat
- Openfire在使用MySQL数据库后的中文乱码问题解决
- Network planning | [five transport layers and six application layers] knowledge points and examples
- 计网 | 【五 传输层、六 应用层】知识点及例题
- How unity pulls one of multiple components
- All the important spark summit features were released here last night (with ultra clear video attached)
猜你喜欢

exness:流动性系列-流动性清洗和反转、决策区间

Primary school, session 3 - afternoon: Web_ sessionlfi

左值引用和右值引用

漏洞扫描工具大全,妈妈再也不用担心我挖不到漏洞了

Implementation principle of PostgreSQL heap table storage engine

大神詳解開源 BUFF 增益攻略丨直播

NLP技能树学习路线-(一)路线总览

Big God explains open source buff gain strategy live broadcast

Maya house modeling

昨晚 Spark Summit 重要功能发布全在这里(附超清视频)
随机推荐
杰理之触摸按键识别流程【篇】
Installation and use of securecrtportable
Big God explains open source buff gain strategy live broadcast
Enterprise middle office planning and it architecture microservice transformation
Notes on modification of Jerry's test box pairing software [chapter]
Heartbeat 与DRBD 配置过程
NLP paper lead reading | what about the degradation of text generation model? Simctg tells you the answer
To eliminate bugs, developers must know several bug exploration and testing artifacts.
QT QStringList用法
黑苹果 服务器系统安装教程,黑苹果安装教程,详细教您黑苹果怎么安装[通俗易懂]
Jenkins can't pull the latest jar package
CADD course learning (2) -- target crystal structure information
Originpro 2021 with installation tutorial
北京大学ACM Problems 1000:A+B Problem
[ICLR 2021] semi supervised object detection: unbiased teacher for semi supervised object detection
MySQL数据库误删回滚的解决
分析超700万个研发需求发现,这八大编程语言才是行业最需要的
How to pass the PMP Exam quickly?
Static classes use @resource annotation injection
杰理之检测灵敏度级别确定【篇】