当前位置:网站首页>PHP require/include differences
PHP require/include differences
2022-06-30 20:32:00 【The smell of tobacco】
Preface
stay PHP in , Loading files can optionally use require, You can also use include, So what's the difference between them ? Read some articles on the Internet , Say they use different scenarios , require Generally, the file is introduced at the beginning of the file , include Generally, files are dynamically introduced into functions .
But I don't think it's that simple , require Is as a language structure ( keyword ) The emergence of , Keywords are generally the less the better for language designers , Keywords are simple without language . So I decided to go deep into the source code level to improve their differences .
To explore the
By looking at , require/require_once/include/include_once These methods are the same after translation c function ZEND_INCLUDE_OR_EVAL_SPEC_CONST_HANDLER.
How to get this c Functions can be found in this article : https://hujingnb.com/archives/836
Check the method :

include/require
include/require Methods are handled in the same way . Let's look at their differences first , Let's see _once Differences in methods .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-2bjPcQLE-1656157366388)(https://oss-blog.cdn.hujingnb.com/img/202206251901506.png)]
By looking for , include And require The only difference , When the file does not exist, the processing method is different .

Search again , The processing method for both is shown in the figure ( This error report is indeed a copy when the document does not exist ). The functions of the two methods called are as follows :
php_error_docref: Throw outphpLevel error . Because what is thrown herewarningLevel , It will not stop the script .zend_throw_error: Throw a compile time exception , Will terminate the script execution .
therefore , Through comparison, we can see , require and include The only difference , When the file does not exist , require Will terminate the script execution , include Throw only warning error .
Here it is zend_throw_error The following comments are seen in the method , It mentions that compile time errors cannot currently be converted to exception. May support in the future ??

include_once/require_once
By looking at the source code , include_once/require_once The difference between , And include/require equally , They are different in handling exceptions , The others are exactly the same

Conclusion
therefore , include and require The only difference , When the file does not exist , require Will terminate the process , and include Can't .
ah , this , It's really good to have two similar keywords , if , take require Get rid of , And then through include Return value to determine whether the file exists , It is up to the caller to decide whether to terminate the process , Would this be more reasonable ? Also, or PHP Our designers are motivated by other considerations ?
Original address : https://hujingnb.com/archives/837
边栏推荐
- 杰理之关于长按开机检测抬起问题【篇】
- NLP skill tree learning route - (I) route overview
- Heartbeat uses NFS to make MySQL highly available based on CRM
- obsidian配合hugo的使用,让markdown本地编辑软件与在线化无缝衔接
- Go language identifier and package name specification
- Exness: the final value of US GDP unexpectedly accelerated to shrink by 1.6%
- 北京大学ACM Problems 1006:Biorhythms
- Wechat applet development practice cloud music
- Jerry's touch key recognition process [chapter]
- Cv+deep learning network architecture pytoch recurrence series basenets (backbones) (I)
猜你喜欢

Wechat applet development practice cloud music
![Jerry's touch key recognition process [chapter]](/img/cf/8dacbb7f80e427276df6201dddd377.png)
Jerry's touch key recognition process [chapter]

Jenkins can't pull the latest jar package

A complete collection of vulnerability scanning tools. Mom doesn't have to worry that I won't find any more vulnerabilities

Golang应用 ━━ 安装、配置与使用hugo博客系统

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

25:第三章:开发通行证服务:8:【注册/登录】接口:接收并校验“手机号和验证码”参数;(重点需要知道【利用redis来暂存数据,获取数据的】的应用场景)(使用到了【@Valid注解】参数校验)

神经网络入门(上)

SQL优化

exness:美GDP终值意外加速萎缩1.6%
随机推荐
maya房子建模
Originpro 2021 with installation tutorial
项目经理不应该犯的错误
originpro 2021 附安装教程
Exness: the final value of US GDP unexpectedly accelerated to shrink by 1.6%
基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
Static classes use @resource annotation injection
Perl转换文件的编码类型
黑苹果 服务器系统安装教程,黑苹果安装教程,详细教您黑苹果怎么安装[通俗易懂]
项目经理面试常见问题及回答技巧
DEX file parsing - Method_ IDS resolution
Network planning | [five transport layers and six application layers] knowledge points and examples
北京大学ACM Problems 1001:Exponentiation
NLP paper lead reading | what about the degradation of text generation model? Simctg tells you the answer
NLP skill tree learning route - (I) route overview
Go学习笔记
Golang应用 ━━ 安装、配置与使用hugo博客系统
Introduction to neural network (Part 1)
Web主机iptables防火墙安全脚本
Web host iptables firewall security script