


[PHP source code reading] empty and isset functions, emptyisset_PHP tutorial
[PHP source code reading] empty and isset functions, emptyisset
I was recently asked how to judge variables when using the empty and isset functions in PHP. I was confused at first. , because I only have a partial understanding of it. In order to understand its true principle, I quickly opened the source code to study it. After analysis, it can be found that both functions call the same function, so this article will analyze the two functions together.
I have more detailed annotations on the PHP source code on github. If you are interested, you can take a look and give it a star. PHP5.4 source code annotations. You can view the added annotations through the commit record.
Function usage format
empty
<p>bool empty ( mixed $var )</p>
Determine whether the variable is empty.
isset
<p>bool isset ( mixed $var [ , mixed $... ] )</p>
Determine whether the variable is set and not NULL.
Parameter description
For empty, before PHP5.5, empty only supports variable parameters. Other types of parameters will cause parsing errors. For example, the result of a function call cannot be used as a parameter.
For isset, if the variable is set to NULL by a function such as unset, the function will return false. If multiple parameters are passed to the isset function, the isset function will return true only if all parameters are set. Calculate from left to right, stopping as soon as an unset variable is encountered.
Run the example
<span>$result</span> = <span>empty</span>(0); <span>//</span><span> true</span> <span>$result</span> = <span>empty</span>(<span>null</span>); <span>//</span><span> true<br /></span><span>$result</span> = <span>empty</span>(<span>false</span>); <span>//</span><span> true</span> <span>$result</span> = <span>empty</span>(<span>array</span>()); <span>//</span><span> true</span> <span>$result</span> = <span>empty</span>('0'); <span>//</span><span> true</span> <span>$result</span> = <span>empty</span>(1); <span>//</span><span> false</span> <span>$result</span> = <span>empty</span>(<span>callback function</span>); <span>//</span><span> 报错<br /><br />$a = null;<br />$result = isset($a); // false;<br /><br />$a = 1;<br />$result = isset($a); // true;<br /><br />$a = 1;$b = 2;$c = 3;<br />$result = isset($a, $b, $c); // true<br /><br /></span>
$a = 1;$b = null;$c = 3;<br />$result = isset($a, $b, $c); // false
Find the location where the function is defined
Actually, empty is not a function, but a language construct. The language structure is compiled before the PHP program is run, so you cannot simply search for "PHP_FUNCTION empty" or "ZEND_FUNCTION empty" to view its source code as before. If you want to see the source code of language structures such as empty, you must first understand the mechanism of PHP code execution.
PHP execution code will go through 4 steps, and the flow chart is as follows:
1. Analysis parameters 2. Check whether it is a writable variable 3. If the op_type of the variable is IS_CV (compile-time variable), set its opcode to ZEND_ISSET_ISEMPTY_VAR; otherwise, get the next op value from active_op_array and set the opcode of last_op according to its op value. 4. After setting the opcode, it will be handed over to zend_excute for execution. IS_CV is a cache mechanism used by the compiler. This variable stores the address of the variable it is referenced. When a variable is referenced for the first time, it will be CVd. In the future, the reference of this variable will be There is no need to look up the active symbol table anymore. For the empty function, after reaching the opcode step, refer to the opcode processing function. You can know that isset and empty execute a series of functions such as ZEND_ISSET_ISEMPTY_VAR when excute is executed. >For example, find the definition of this function in zend_vm_execute.h. Looking at the function, we can see that the final execution function of the empty function is i_zend_is_true(), and the i_zend_is_true function is defined in zend_execute.h. The core code of the i_zend_is_true function is as follows:
empty(null), go to the IS_NULL branch, result =0, i_zend_is_true() == 0, !i_zend_is_true() == 1, so true is returned. empty(array()), to IS_ARRAY branch, result = zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0), zend_hash_num_elements returns the number of array elements, array is empty, so result is 0, i_zend_is_true() == 0, !i_zend_is_true() == 1, so returns true. empty('0'), to the IS_STRING branch, because Z_STRLENP(op) == 1 and Z_STRVAL_P(op)[0] == '0', so the result is 0, i_zend_is_true() == 0, !i_zend_is_true () == 1, so returns true. empty(1), to IS_LONG branch, result = Z_LVAL_P(op) = 1, i_zend_is_true == 1, !i_zend_is_true() == 0, so false is returned. For the isset function, the final code to implement the judgment is:
Summary 1. Execution steps of PHP code during compilation 2. How to find the source code location of PHP language structure 3. How to find the specific function of the opcode processing function There is no end to learning. Everyone has their own shortcomings. Only through continuous learning can we make up for our shortcomings. Original article with limited writing style and limited knowledge. If there is anything wrong in the article, please let me know. If this article is helpful to you, please click to recommend it, thank you^_^ Finally, I have more detailed annotations on the PHP source code on github. If you are interested, you can take a look and give it a star. PHP5.4 source code annotations. You can view the added annotations through the commit record. For more source code articles, please visit your personal homepage to continue viewing: hoohack
Then comes the Parsing stage. In this stage, the program converts Tokens such as T_ISSET and T_EMPTY into meaningful expressions. Syntax analysis will be done at this time. The yacc of the Tokens is saved in the zend_language_parser.y file. You can Find the definitions of T_ISSET and T_EMPTY: <span>internal_functions_in_yacc:
T_ISSET </span><span>'</span><span>(</span><span>'</span> isset_variables <span>'</span><span>)</span><span>'</span> { $$ = $<span>3</span><span>; }
</span>| T_EMPTY <span>'</span><span>(</span><span>'</span> variable <span>'</span><span>)</span><span>'</span> { zend_do_isset_or_isempty(ZEND_ISEMPTY, &$$, &$<span>3</span><span> TSRMLS_CC); }
</span>| T_INCLUDE expr { zend_do_include_or_eval(ZEND_INCLUDE, &$$, &$<span>2</span><span> TSRMLS_CC); }
</span>| T_INCLUDE_ONCE expr { zend_do_include_or_eval(ZEND_INCLUDE_ONCE, &$$, &$<span>2</span><span> TSRMLS_CC); }
</span>| T_EVAL <span>'</span><span>(</span><span>'</span> expr <span>'</span><span>)</span><span>'</span> { zend_do_include_or_eval(ZEND_EVAL, &$$, &$<span>3</span><span> TSRMLS_CC); }
</span>| T_REQUIRE expr { zend_do_include_or_eval(ZEND_REQUIRE, &$$, &$<span>2</span><span> TSRMLS_CC); }
</span>| T_REQUIRE_ONCE expr { zend_do_include_or_eval(ZEND_REQUIRE_ONCE, &$$, &$<span>2</span><span> TSRMLS_CC); }
;</span>
Both the isset and empty functions eventually execute the zend_do_isset_or_isempty function. Continue to search
grep -rn "zend_do_isset_or_isempty"
and you can find that this function is defined in the zend_compile.c file. Function execution steps
Source code interpretation <span>switch</span><span> (Z_TYPE_P(op)) {
</span><span>case</span><span> IS_NULL:
result </span>= <span>0</span><span>;
</span><span>break</span><span>;
</span><span>case</span><span> IS_LONG:
</span><span>case</span><span> IS_BOOL:
</span><span>case</span><span> IS_RESOURCE:
</span><span>//</span><span> empty参数为整数时非0的话就为false</span>
result = (Z_LVAL_P(op)?<span>1</span>:<span>0</span><span>);
</span><span>break</span><span>;
</span><span>case</span><span> IS_DOUBLE:
result </span>= (Z_DVAL_P(op) ? <span>1</span> : <span>0</span><span>);
</span><span>break</span><span>;
</span><span>case</span><span> IS_STRING:
</span><span>if</span> (Z_STRLEN_P(op) == <span>0</span>
|| (Z_STRLEN_P(op)==<span>1</span> && Z_STRVAL_P(op)[<span>0</span>]==<span>'</span><span>0</span><span>'</span><span>)) {
</span><span>//</span><span> empty("0") == true</span>
result = <span>0</span><span>;
} </span><span>else</span><span> {
result </span>= <span>1</span><span>;
}
</span><span>break</span><span>;
</span><span>case</span><span> IS_ARRAY:
</span><span>//</span><span> empty(array) 是根据数组的数量来判断</span>
result = (zend_hash_num_elements(Z_ARRVAL_P(op))?<span>1</span>:<span>0</span><span>);
</span><span>break</span><span>;
</span><span>case</span><span> IS_OBJECT:
</span><span>if</span>(IS_ZEND_STD_OBJECT(*<span>op)) {
TSRMLS_FETCH();
</span><span>if</span> (Z_OBJ_HT_P(op)-><span>cast_object) {
zval tmp;
</span><span>if</span> (Z_OBJ_HT_P(op)->cast_object(op, &tmp, IS_BOOL TSRMLS_CC) ==<span> SUCCESS) {
result </span>=<span> Z_LVAL(tmp);
</span><span>break</span><span>;
}
} </span><span>else</span> <span>if</span> (Z_OBJ_HT_P(op)-><span>get</span><span>) {
zval </span>*tmp = Z_OBJ_HT_P(op)-><span>get</span><span>(op TSRMLS_CC);
</span><span>if</span>(Z_TYPE_P(tmp) !=<span> IS_OBJECT) {
</span><span>/*</span><span> for safety - avoid loop </span><span>*/</span><span>
convert_to_boolean(tmp);
result </span>=<span> Z_LVAL_P(tmp);
zval_ptr_dtor(</span>&<span>tmp);
</span><span>break</span><span>;
}
}
}
result </span>= <span>1</span><span>;
</span><span>break</span><span>;
</span><span>default</span><span>:
result </span>= <span>0</span><span>;
</span><span>break</span><span>;
}</span>
empty(false), to the IS_BOOL branch, result = ZLVAL_P(false) = 0, i_zend_is_true() == 0, !i_zend_is_true() == 1, so return true. if (isset && Z_TYPE_PP(value) !=<span> IS_NULL) {
ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 1<span>);
} else<span> {
ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 0<span>);
}</span></span></span></span>
This time I read the source code of these two functions and learned:
Reference article
Opcode processing function search: http://www.laruence.com/2008/06/18/221.html
In-depth understanding of PHPopcode and PHP code execution steps: http:/ /www.php-internals.com/book/?p=chapt02/02-03-03-from-opcode-to-handler

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
