In-depth understanding of the empty and isset functions in PHP, emptyisset_php tutorial

Source: Internet
Author: User
Tags php language php source code

In-depth understanding of the empty and isset functions in PHP, Emptyisset


Recently asked about the PHP empty and isset function how to determine the variables, just at the beginning I was a face, because I was just smattering, in order to understand its true principle, hurriedly opened the source research research. The analysis reveals that all two function calls are the same function, so this article will analyze the two functions together.

I have a more detailed comment on the PHP source code on GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation. Comments that have been added can be viewed through a commit record.

function Use format

Empty

BOOL Empty (mixed $var)

Determines whether the variable is empty.

Isset

BOOL Isset (mixed $var [, mixed $ ...]

Determines whether the variable is set and is not NULL.

Parameter description

For empty, before the PHP5.5 version, empty only supports variable arguments, and other types of arguments result in parsing errors, such as the result of a function call that cannot be used as a parameter.

For Isset, if a variable is set to NULL by a function such as unset, the function returns FALSE. If more than one parameter is passed to the Isset function, only the Isset function will return TRUE if all parameters are set. From left to right, once you have encountered a variable that has not been set, stop.

Run the sample

$result = Empty (0); True$result = empty (null); True$result = Empty (false); True$result = Empty (Array ()); True$result = Empty (' 0 '); True$result = Empty (1); False$result = Empty (callback function); Error $ A = null; $result = Isset ($a); false; $a = 1; $result = isset ($a); true; $a = 1; $b = 2; $c = 3; $result = Isset ($a, $b, $c); true$a = 1; $b = null; $c = 3; $result = Isset ($a, $b, $c); False

Locate the defined position of the function

In fact, empty is not a function, but a language structure. The language structure is compiled before the PHP program is run, so you can't simply search for "php_function empty" or "zend_function empty" to see its source as it did before. To see the source of language structure such as empty, first understand the PHP code execution mechanism.

PHP executes the code in 4 steps, and the flowchart is as follows:


In the first stage, the scanning phase, the program scans the ZEND_LANGUAGE_SCANNER.L file to convert the code file into a language fragment. For the isset and empty functions, searching for empty and isset in the Zend_language_scanner.l file can get the function of the macro defined in this file as follows:

 
  
   
  "Isset" {return t_isset;}
  
   
    
   "Empty" {return t_empty;}
  
   
 
  

Next to the parsing stage, this stage, the program will T_isset and T_empty and other tokens into meaningful expressions, this time will do the parsing, tokens Yacc saved in Zend_language_ PARSER.Y file, you can find definitions of t_isset and T_empty

Internal_functions_in_yacc:t_isset ' (' isset_variables ') ' {$$ = $;}| T_empty ' (' variable ') ' {zend_do_isset_or_isempty (Zend_isempty, &$$, &$3 tsrmls_cc);}| T_include Expr {zend_do_include_or_eval (zend_include, &$$, &$2 tsrmls_cc);}| T_include_once Expr {zend_do_include_or_eval (zend_include_once, &$$, &$2 tsrmls_cc);}| T_eval ' (' expr ') ' {zend_do_include_or_eval (Zend_eval, &$$, &$3 tsrmls_cc);}| T_require Expr {zend_do_include_or_eval (Zend_require, &$$, &$2 tsrmls_cc);}| T_require_once Expr {zend_do_include_or_eval (zend_require_once, &$$, &$2 tsrmls_cc);};

The isset and empty functions eventually execute the Zend_do_isset_or_isempty function, and continue to find

Grep-rn "Zend_do_isset_or_isempty"

It can be found that this function is defined in the zend_compile.c file.

function execution Steps

1. Analytical parameters

2. Check if it is a writable variable

3, if the op_type of the variable is IS_CV (the compiler time variable), then set its opcode to zend_isset_isempty_var; otherwise get the next op value from Active_op_array, set last_ according to its OP value op's opcode.

4, after setting up the opcode, then will be handed over to zend_excute execution.

SOURCE Interpretation

IS_CV is a cache mechanism used by the compiler, which holds the address of the variable it refers to, and when a variable is referenced for the first time, it is CV-the reference to the variable does not need to look up the active symbol table again.

For the empty function, after the steps of opcode, refer to the opcode handler function, you can know that ISSET and empty are executed in Excute Zend_isset_isempty_var a series of functions to Zend_isset_ Isempty_var_spec_cv_var_handler, for example, finds the definition of this function in Zend_vm_execute.h. The view function knows that the final execution function of the empty function is i_zend_is_true (), whereas 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:

Switch (z_type_p (OP)) {case Is_null:result = 0;    Break      Case Is_long:case is_bool:case Is_resource://The empty argument is an integer when not 0 is false result = (z_lval_p (OP)? 1:0);    Break      Case Is_double:result = (z_dval_p (OP)? 1:0);    Break Case Is_string:if (z_strlen_p (OP) = = 0 | | (Z_strlen_p (OP) ==1 && z_strval_p (OP) [0]== ' 0 '))      {//Empty ("0") = = true result = 0;      } else {result = 1;    } break;      Case Is_array://Empty (array) is based on the number of arrays to determine the result = (Zend_hash_num_elements (z_arrval_p (OP))? 1:0);    Break        Case Is_object:if (Is_zend_std_object (*op)) {Tsrmls_fetch ();          if (z_obj_ht_p (OP)->cast_object) {Zval tmp;            if (z_obj_ht_p (OP)->cast_object (OP, &tmp, is_bool tsrmls_cc) = = SUCCESS) {result = Z_lval (TMP);          Break }} else if (z_obj_ht_p (OP)->get) {Zval *tmp = z_obj_ht_p (OP)->get (Op tsrmls_CC);            if (z_type_p (tmp) = Is_object) {/* for Safety-avoid Loop */Convert_to_boolean (TMP);            result = Z_lval_p (TMP);            Zval_ptr_dtor (&TMP);          Break      }}} result = 1;    Break      Default:result = 0;  Break }

This code is more intuitive, the function does not make any conversion of the detection value, through this code to further analyze the example of the empty function to do the analysis:
Empty (null), to Is_null Branch, result=0,i_zend_is_true () = = 0,!i_zend_is_true () = = 1, so returns TRUE.

Empty (false), to the Is_bool branch, result = Zlval_p (false) = 0,i_zend_is_true () = = 0,!i_zend_is_true () = = 1, so returns 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 Is_string branch, because Z_STRLENP (OP) = = 1 and z_strval_p (OP) [0] = = ' 0 ', so 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, therefore returns false.

For the Isset function, the code that ultimately implements the judgment is:

if (isset && z_type_pp (value)! = Is_null) {  Zval_bool (&ex_t (Opline->result.var). Tmp_var, 1);} else {  Zval_bool (&ex_t (Opline->result.var). Tmp_var, 0);}

Returns true as long as value is set and is not a null,isset function.

Summary

This reading of the two functions of the source code, learning to:

1, the PHP code during the execution of the procedure

2. How to find the source location of PHP language structure

3. How to find the specific function of opcode processing function

Lifelong learning, everyone has their own short board, only through continuous study to make their own short board to fill.

Original article, writing Limited, Caishuxueqian, if there is not in the text, million hope to inform.

If this article is helpful to you, please click on the recommendation, thank you ^_^

This in-depth understanding of PHP in the empty and isset function is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we have a lot of support to help guests home.

http://www.bkjia.com/PHPjc/1133076.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133076.html techarticle in-depth understanding of PHP in the empty and Isset functions, Emptyisset recently asked about the empty and isset functions in PHP How to determine the variables, just at the beginning I was a face confused, because I am just a know ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.