当前位置:网站首页>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 outphp
Level error . Because what is thrown herewarning
Level , 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
边栏推荐
猜你喜欢
左值引用和右值引用
exness:美GDP终值意外加速萎缩1.6%
Golang应用 ━━ 安装、配置与使用hugo博客系统
基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
obsidian配合hugo的使用,让markdown本地编辑软件与在线化无缝衔接
谈谈内联函数
建立自己的网站(20)
哈夫曼树(一)基本概念与C语言实现
NLP 论文领读|文本生成模型退化怎么办?SimCTG 告诉你答案
Network planning | [five transport layers and six application layers] knowledge points and examples
随机推荐
杰理之触摸按键识别流程【篇】
Black apple server system installation tutorial, black apple installation tutorial, teach you how to install black apple in detail [easy to understand]
1、生成对抗网络入门
DEX file parsing - Method_ IDS resolution
exness:流动性系列-流动性清洗和反转、决策区间
Encoding type of Perl conversion file
杰理之触摸按键识别流程【篇】
哈夫曼樹(一)基本概念與C語言實現
杰理之关于长按复位【篇】
Jerry's question about long press boot detection [chapter]
Jerry's touch key recognition process [chapter]
谈谈内联函数
A complete collection of vulnerability scanning tools. Mom doesn't have to worry that I won't find any more vulnerabilities
Introduction to neural network (Part 1)
Common questions and answering skills of project manager interview
Static classes use @resource annotation injection
北京大学ACM Problems 1003:Hangover
DEX文件解析 - method_ids解析
pytorch实现FLOPs和Params的计算
北京大学ACM Problems 1000:A+B Problem