[PHP source code reading] empty and isset functions, emptyisset_PHP tutorial

WBOY
Release: 2016-07-12 08:51:37
Original
668 people have browsed it

[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>
Copy after login


Determine whether the variable is empty.

isset

<p>bool isset ( mixed $var [ , mixed $... ] )</p>


Copy after login

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>
Copy after login
$a = 1;$b = null;$c = 3;<br />$result = isset($a, $b, $c); // false
Copy after login

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:

[PHP source code reading] empty and isset functions, emptyisset_PHP tutorial"isset" { return T_ISSET; } "empty" { return T_EMPTY; }


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>
Copy after login


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

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.


Source code interpretation

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:

        <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>
Copy after login
This code is relatively intuitive. The function does not perform any conversion on the detection value. Use this code to further analyze the empty function in the example:

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(false), to the IS_BOOL branch, result = ZLVAL_P(false) = 0, i_zend_is_true() == 0, !i_zend_is_true() == 1, so return true.

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:

As long as value is set and is not NULL, the isset function returns 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>
Copy after login

Summary

This time I read the source code of these two functions and learned:

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.


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

For more source code articles, please visit your personal homepage to continue viewing: hoohack

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1129313.htmlTechArticle[PHP source code reading] empty and isset functions, emptyisset Recently I was asked how to judge the empty and isset functions in PHP It’s variable. I was confused at first because I only knew a little about it...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template