search
HomeBackend DevelopmentPHP Tutorial分析PHP内核中是如何实现 empty, isset 这些函数的

#### 叨叨几句本来这个问题是在oschina上提出的:  <http://www.oschina.net/question/1179015_2140695>但一直没收到合适的答案,所以还是自己下功夫梳理了一下,如果有错误的地方,欢迎交流。通常的函数是通过ZEND_FUNCTION(xxx) 这种宏定义来实现的,这个规范很好理解,也很容易读懂源码。但empty(), isset() 准确的说不是函数,但PHP的Manual还是称之函数,类似的还有echo, eval等。#### 准备工作用于查看PHP的扩展vld,下载:  <http://pecl.php.net/package/vld>PHP源码,分支 => remotes/origin/PHP-5.6.14	git clone http://git.php.net/repository/php-src.git -b PHP-5.6.14PHP opcode对应参考:  <http://php.net/manual/en/internals2.opcodes.php>> PHP执行程序版本为 5.6.14 ,其他版本opcode可能会有细微差别。PHP 内核源码分析:  <http://www.php-internals.com/book/>#### 开始分析示例代码 vld.php :	<?php		$a = 0;		empty($a);		isset($a);通过vld 查看opcode ,`php -d vld.active=1 vld.php`	number of ops:  10	compiled vars:  !0 = $a	line     #* E I O op                           fetch          ext  return  operands	-------------------------------------------------------------------------------------	   2     0  E >   EXT_STMT			 1        ASSIGN                                                   !0, 0	   3     2        EXT_STMT			 3        ISSET_ISEMPTY_VAR                           293601280  ~1      !0			 4        FREE                                                     ~1	   4     5        EXT_STMT			 6        ISSET_ISEMPTY_VAR                           310378496  ~2      !0			 7        FREE                                                     ~2	   6     8        EXT_STMT			 9      > RETURN                                                   1	branch: #  0; line:     2-    6; sop:     0; eop:     9; out1:  -2opcode中都出现了ZEND_ISSET_ISEMPTY_VAR,我们一步步分析。当执行PHP源码,会先进行语法分析,empty, isset的yacc如下:vim Zend/zend_language_parser.y +1265	1265 internal_functions_in_yacc:	1266 ›   ›   T_ISSET '(' isset_variables ')' { $$ = $3; }	1267 ›   |›  T_EMPTY '(' variable ')'›   { zend_do_isset_or_isempty(ZEND_ISEMPTY, &$$, &$3 TSRMLS_CC); }	1275	1276 isset_variables:	1277 ›   ›   isset_variable› ›   ›   { $$ = $1; }	1280	1281 isset_variable:	1282 ›   ›   variable›   ›   ›   ›   { zend_do_isset_or_isempty(ZEND_ISSET, &$$, &$1 TSRMLS_CC); }最终都执行了zend_do_isset_or_isempty,继续查找:	git grep -in "zend_do_isset_or_isempty"	Zend/zend_compile.c:6287:void zend_do_isset_or_isempty(int type, znode *result, znode *variable TSRMLS_DC) /* {:{:{ */vi Zend/zend_compile.c +6287	6287 void zend_do_isset_or_isempty(int type, znode *result, znode *variable TSRMLS_DC) /* {{{ */	6288 {	6289 ›   zend_op *last_op;	6290	6291 ›   zend_do_end_variable_parse(variable, BP_VAR_IS, 0 TSRMLS_CC);	6292	6293 ›   if (zend_is_function_or_method_call(variable)) {	6294 ›   ›   if (type == ZEND_ISEMPTY) {	6295 ›   ›   ›   /* empty(func()) can be transformed to !func() */	6296 ›   ›   ›   zend_do_unary_op(ZEND_BOOL_NOT, result, variable TSRMLS_CC);	6297 ›   ›   } else {	6298 ›   ›   ›   zend_error_noreturn(E_COMPILE_ERROR, "Cannot use isset() on the result of a function call (you can use \"null !== func()\" instead)");	6299 ›   ›   }	6300	6301 ›   ›   return;	6302 ›   }	6303	6304 ›   if (variable->op_type == IS_CV) {	6305 ›   ›   last_op = get_next_op(CG(active_op_array) TSRMLS_CC);	6306 ›   ›   last_op->opcode = ZEND_ISSET_ISEMPTY_VAR;最后一行 6306,ZEND_ISSET_ISEMPTY_VAR 这个opcode 出来了,IS_CV 判断参数是否为变量。注意zend_is_function_or_method_call(variable),当isset(fun($a)),函数参数写法会报错,empty在5.5版本开始支持函数参数,低版本不支持。opcode 是由 zend_execute 执行的,最终会对应处理函数的查找,这个是核心,请参阅:  <http://www.php-internals.com/book/?p=chapt02/02-03-03-from-opcode-to-handler>opcode 对应处理函数的命名规律:	ZEND_[opcode]_SPEC_(变量类型1)_(变量类型2)_HANDLER变量类型1和变量类型2是可选的,如果同时存在,那就是左值和右值,归纳有下几类: VAR TMP CV UNUSED CONST 这样可以根据相关的执行场景来判定。所以 ZEND_ISSET_ISEMPTY_VAR 对应的handler如下:	Zend/zend_vm_execute.h:44233:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CONST_CONST_HANDLER,	Zend/zend_vm_execute.h:44235:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CONST_VAR_HANDLER,	Zend/zend_vm_execute.h:44236:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CONST_UNUSED_HANDLER,	Zend/zend_vm_execute.h:44238:   ZEND_ISSET_ISEMPTY_VAR_SPEC_TMP_CONST_HANDLER,	Zend/zend_vm_execute.h:44240:   ZEND_ISSET_ISEMPTY_VAR_SPEC_TMP_VAR_HANDLER,	Zend/zend_vm_execute.h:44241:   ZEND_ISSET_ISEMPTY_VAR_SPEC_TMP_UNUSED_HANDLER,	Zend/zend_vm_execute.h:44243:   ZEND_ISSET_ISEMPTY_VAR_SPEC_VAR_CONST_HANDLER,	Zend/zend_vm_execute.h:44245:   ZEND_ISSET_ISEMPTY_VAR_SPEC_VAR_VAR_HANDLER,	Zend/zend_vm_execute.h:44246:   ZEND_ISSET_ISEMPTY_VAR_SPEC_VAR_UNUSED_HANDLER,	Zend/zend_vm_execute.h:44253:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CV_CONST_HANDLER,	Zend/zend_vm_execute.h:44255:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CV_VAR_HANDLER,	Zend/zend_vm_execute.h:44256:   ZEND_ISSET_ISEMPTY_VAR_SPEC_CV_UNUSED_HANDLER,我们看下 ZEND_ISSET_ISEMPTY_VAR_SPEC_CV_VAR_HANDLER 这个处理函数:vim Zend/zend_vm_execute.h +37946	38013 ›   if (opline->extended_value & ZEND_ISSET) {	38014 ›   ›   if (isset && Z_TYPE_PP(value) != IS_NULL) {	38015 ›   ›   ›   ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 1);	38016 ›   ›   } else {	38017 ›   ›   ›   ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 0);	38018 ›   ›   }	38019 ›   } else /* if (opline->extended_value & ZEND_ISEMPTY) */ {	38020 ›   ›   if (!isset || !i_zend_is_true(*value)) {	38021 ›   ›   ›   ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 1);	38022 ›   ›   } else {	38023 ›   ›   ›   ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 0);	38024 ›   ›   }上面的 if ... else 就是判断是isset,还是empty,然后做不同处理,Z_TYPE_PP, i_zend_is_true 不同判断。echo 等处理类似,自己按照流程具体去分析。关键是根据映射表找到对应的handler处理函数。了解这些处理流程后,相信会对PHP语句的性能分析更熟悉。原文出自[老刺猬](http://www.yinqisen.cn),<http://www.yinqisen.cn/blog-680.html>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools