当前位置:网站首页>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
边栏推荐
- Cv+deep learning network architecture pytoch recurrence series basenets (backbones) (I)
- 杰理之关于长按开机检测抬起问题【篇】
- Taihu Lake "China's healthy agricultural products · mobile phone live broadcast" enters Taihu Lake
- 网上炒股开户安全嘛!?
- Jerry's touch key recognition process [chapter]
- The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's first disciple
- 昨晚 Spark Summit 重要功能发布全在这里(附超清视频)
- Is it safe to open an account in Guangzhou stock exchange by mobile phone?
- 大神详解开源 BUFF 增益攻略丨直播
- 杰理之检测灵敏度级别确定【篇】
猜你喜欢

基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享

All the important spark summit features were released here last night (with ultra clear video attached)

Document contains & conditional competition

昨晚 Spark Summit 重要功能发布全在这里(附超清视频)

Lambda 表达式原理分析学习(2022.06.23)

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

左值引用和右值引用

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

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

漏洞扫描工具大全,妈妈再也不用担心我挖不到漏洞了
随机推荐
杰理之关于长按复位【篇】
静态类使用@Resource注解注入
昨晚 Spark Summit 重要功能发布全在这里(附超清视频)
文件包含&条件竞争
AVL平衡二叉树(一) - 概念与C语言实现
Tensorflow2.4 implementation of repvgg
Summary of PHP file upload (garbled code, move failure, permission, display picture)
Jerry's touch key recognition process [chapter]
Filebeat custom indexes and fields
Cv+deep learning network architecture pytoch recurrence series basenets (backbones) (I)
断点续传和下载原理分析
Exness: liquidity series - liquidity cleaning and reversal, decision interval
大神详解开源 BUFF 增益攻略丨直播
AVL balanced binary tree (I) - concept and C language implementation
杰理之关于长按开机检测抬起问题【篇】
maya房子建模
Summary of personal work of 21 groups in the first week of summer training
mysql登录出现1045错误修改方法[通俗易懂]
PHP require/include 区别
Evolution of screen display technology