PHP Tutorial: Variable life cycle in PHP programming

Source: Internet
Author: User
Tags apc array arrays execution final hash variables strlen

There are two types of data sources in PHP:

 
  
  
  1. 1. From the Code

For variables in the code (that is, the direct amount), the variable assignment/assignment is active in the executor at compile time, and is destroyed during the request shutdown period. For these variables, the value of this part of the variable is cached by using APC for opcode caching.

And for variables from the outside, variable allocation/assignment in the compiler, before the execution period, in the request shutdown period was destroyed, for these variables, using APC for opcode cache, is not cached.

Focus today on one part of the next external variable, the entire lifecycle of the data from get.

Suppose the following request arrives:

 
  
  

And, in the index.php:

 
 
  1. <?php
  2. $name =

    There are two types of data sources in PHP:

     
         
         
    1. 1. From the Code

    For variables in the code (that is, the direct amount), the variable assignment/assignment is active in the executor at compile time, and is destroyed during the request shutdown period. For these variables, the value of this part of the variable is cached by using APC for opcode caching.

    And for variables from the outside, variable allocation/assignment in the compiler, before the execution period, in the request shutdown period was destroyed, for these variables, using APC for opcode cache, is not cached.

    Focus today on one part of the next external variable, the entire lifecycle of the data from get.

    Suppose the following request arrives:

     
         
         

    And, in the index.php:

    ___fckpd___2

    We know that in the final execution period, the $_get array must contain the following fragment:

        
        
    1. There are two types of data sources in PHP:

       
              
              
      1. 1. From the Code

      For variables in the code (that is, the direct amount), the variable assignment/assignment is active in the executor at compile time, and is destroyed during the request shutdown period. For these variables, the value of this part of the variable is cached by using APC for opcode caching.

      And for variables from the outside, variable allocation/assignment in the compiler, before the execution period, in the request shutdown period was destroyed, for these variables, using APC for opcode cache, is not cached.

      Focus today on one part of the next external variable, the entire lifecycle of the data from get.

      Suppose the following request arrives:

       
              
              

      And, in the index.php:

             
             
      1. <?php
      2. $name =

        There are two types of data sources in PHP:

         
                   
                   
        1. 1. From the Code

        For variables in the code (that is, the direct amount), the variable assignment/assignment is active in the executor at compile time, and is destroyed during the request shutdown period. For these variables, the value of this part of the variable is cached by using APC for opcode caching.

        And for variables from the outside, variable allocation/assignment in the compiler, before the execution period, in the request shutdown period was destroyed, for these variables, using APC for opcode cache, is not cached.

        Focus today on one part of the next external variable, the entire lifecycle of the data from get.

        Suppose the following request arrives:

         
                   
                   

        And, in the index.php:

        ___fckpd___2

        We know that in the final execution period, the $_get array must contain the following fragment:

        ___fckpd___3

        So, let's focus today on how Query string is built into _get arrays (for the generation of Get variables, please read my previous article: "PHP's Get/post and other large variable generation process"):

        At the time of the request, the Php_request_startup (defined in MAIN.C) is invoked to do the initialization of the scene. Include setting time-out values in this procedure, calling the request initialization function for each module. Of course, we also care about creating variable environments.

        Php_hash_environment according to the Variables_order in php.ini to initialize each predefined large variable sequentially, then for $_get:

         
                   
                   
        1. ...
        2. case ' g ':
        3. case ' G ':
        4.      if (!_gpc_flags[2]) {
        5.            sapi_module.treat_data (parse_get, NULL, null TSRMLS_CC);
        6.           _gpc_flags[2] = 1;
        7.           if (PG (register_globals)) {
        8.                 php_autoglobal_merge (&EG ( symbol_table),
        9.                      z_arrval_p (PG (http_globals) [track_vars_get]) tsrmls_cc);
        10.           }
        11.      }
        12. break;

        In general, this logic, first through the treat_data to generate variable hash (PG (http_globals) [Track_vars_get]), if you open the Auto_register_globals, then the $_ The variables in the get array are added to the symbol table.

        Treat_data is a member of the Sapi_module_struct:

         
                   
                   
        1. Note: This article is based on the apache2handler approach of SAPI, this startup process and previous articles SAPI
        2. The principle of the startup process is slightly different, php5 through the registered apache2 Ap_hook_post_config Hook,
        3. Start PHP when Apache server is started (Php_apache_server_startup, defined in
        4. SAPI/APACHE2HANDER/SAPI_APACHE2.C), in this function call Sapi_startup start SAPI,
        5. And then by calling Php_apache2_startup to register SAPI module struct,
        6. Then invoke Php_module_startup to initialize PHP, which will initialize the Zend engine,
        7. and fill in the Treat_data member in Zend_module_struct (through Php_startup_sapi_content_types)

        Now go back and look at Treat_data (i.e. Php_default_treat_data):

         
                   
                   
        1. ....
        2. if (arg = = Parse_get) {/* Get data */
        3. C_var = SG (request_info). query_string;
        4. if (C_var && *c_var) {
        5. res = (char *) estrdup (C_var);
        6. Free_buffer = 1;
        7. } else {
        8. Free_buffer = 0;
        9. }
        10. else if (arg = = Parse_cookie) {/* COOKIE data * *

        In the above logic, the res is replicated to Query_string, and the SG (request_info) is a structure representing the current request information, where query_string is in Php_apache_request_ ctor by copying the args in the Apache REQEUST_REC structure.

        For the example of this article, Res is now "Name=laruence&career[]=yahoo&career[]=baidu",

        To continue in the Treat_data, the following logic is:

                  
                  
        1. var = php_strtok_r (res, separator, &strtok_buf);
        2. ...
        3. while (Var) {
        4. val = Strchr (var, ' = ');
        5. if (arg = = Parse_cookie) {
        6. /* Remove leading spaces from cookie names,
        7. Needed for Multi-cookie header where; Can be followed by a space * *
        8. while (Isspace (*var)) {
        9. var++;
        10. }
        11. if (var = val *var = ' ") {
        12. Goto Next_cookie;
        13. }
        14. }
        15. if (val) {/* have a value */
        16. int Val_len;
        17. unsigned int new_val_len;
        18. *val++ = ' n ';
        19. Php_url_decode (Var, strlen (Var));
        20. Val_len = Php_url_decode (Val, strlen (Val));
        21. val = Estrndup (val, Val_len);
        22. if (Sapi_module.input_filter (ARG, VAR, &val, Val_len, &new_val_len tsrmls_cc)) {
        23. Php_register_variable_safe (Var, Val, New_val_len, Array_ptr tsrmls_cc);
        24. }
        25. Efree (Val);
        26. } else {

        First, by php_strtok_r the res into a "Key=value" segment based on "&", then the Var and Val are copied as key and value respectively, noting that the Var and Val are php_url_ in this process. Decode.

        Finally, add a member named Var value of Php_register_variable_safe to Array_ptr (http_globals) [Track_vars_get], or $_get, at this point.

        In this step, our $_get array contains the following members:

         
                   
                   
        1. ' Name ' => ' laruence ',
        2. ' Career ' => array (
        3. ' Yahoo ', ' Baidu ',

        To be continued (the destruction process of the variable) ...

        get[' name '];

      We know that in the final execution period, the $_get array must contain the following fragment:

      ___fckpd___3

      So, let's focus today on how Query string is built into _get arrays (for the generation of Get variables, please read my previous article: "PHP's Get/post and other large variable generation process"):

      At the time of the request, the Php_request_startup (defined in MAIN.C) is invoked to do the initialization of the scene. Include setting time-out values in this procedure, calling the request initialization function for each module. Of course, we also care about creating variable environments.

      Php_hash_environment according to the Variables_order in php.ini to initialize each predefined large variable sequentially, then for $_get:

      ___fckpd___4

      In general, this logic, first through the treat_data to generate variable hash (PG (http_globals) [Track_vars_get]), if you open the Auto_register_globals, then the $_ The variables in the get array are added to the symbol table.

      Treat_data is a member of the Sapi_module_struct:

      ___fckpd___5

      Now go back and look at Treat_data (i.e. Php_default_treat_data):

      ___fckpd___6

      In the above logic, the res is replicated to Query_string, and the SG (request_info) is a structure representing the current request information, where query_string is in Php_apache_request_ ctor by copying the args in the Apache REQEUST_REC structure.

      For the example of this article, Res is now "Name=laruence&career[]=yahoo&career[]=baidu",

      To continue in the Treat_data, the following logic is:

      ___fckpd___7

      First, by php_strtok_r the res into a "Key=value" segment based on "&", then the Var and Val are copied as key and value respectively, noting that the Var and Val are php_url_ in this process. Decode.

      Finally, add a member named Var value of Php_register_variable_safe to Array_ptr (http_globals) [Track_vars_get], or $_get, at this point.

      In this step, our $_get array contains the following members:

      ___fckpd___8

      To be continued (the destruction process of the variable) ...

      Get = Array (
    2. ' Name ' => ' laruence ',
    3. ' Career ' => array (
    4. ' Yahoo ', ' Baidu ',
    5. ),

    So, let's focus today on how Query string is built into _get arrays (for the generation of Get variables, please read my previous article: "PHP's Get/post and other large variable generation process"):

    At the time of the request, the Php_request_startup (defined in MAIN.C) is invoked to do the initialization of the scene. Include setting time-out values in this procedure, calling the request initialization function for each module. Of course, we also care about creating variable environments.

    Php_hash_environment according to the Variables_order in php.ini to initialize each predefined large variable sequentially, then for $_get:

    ___fckpd___4

    In general, this logic, first through the treat_data to generate variable hash (PG (http_globals) [Track_vars_get]), if you open the Auto_register_globals, then the $_ The variables in the get array are added to the symbol table.

    Treat_data is a member of the Sapi_module_struct:

    ___fckpd___5

    Now go back and look at Treat_data (i.e. Php_default_treat_data):

    ___fckpd___6

    In the above logic, the res is replicated to Query_string, and the SG (request_info) is a structure representing the current request information, where query_string is in Php_apache_request_ ctor by copying the args in the Apache REQEUST_REC structure.

    For the example of this article, Res is now "Name=laruence&career[]=yahoo&career[]=baidu",

    To continue in the Treat_data, the following logic is:

    ___fckpd___7

    First, by php_strtok_r the res into a "Key=value" segment based on "&", then the Var and Val are copied as key and value respectively, noting that the Var and Val are php_url_ in this process. Decode.

    Finally, add a member named Var value of Php_register_variable_safe to Array_ptr (http_globals) [Track_vars_get], or $_get, at this point.

    In this step, our $_get array contains the following members:

    ___fckpd___8

    To be continued (the destruction process of the variable) ...

    get[' name '];

We know that in the final execution period, the $_get array must contain the following fragment:

___fckpd___3

So, let's focus today on how Query string is built into _get arrays (for the generation of Get variables, please read my previous article: "PHP's Get/post and other large variable generation process"):

At the time of the request, the Php_request_startup (defined in MAIN.C) is invoked to do the initialization of the scene. Include setting time-out values in this procedure, calling the request initialization function for each module. Of course, we also care about creating variable environments.

Php_hash_environment according to the Variables_order in php.ini to initialize each predefined large variable sequentially, then for $_get:

___fckpd___4

In general, this logic, first through the treat_data to generate variable hash (PG (http_globals) [Track_vars_get]), if you open the Auto_register_globals, then the $_ The variables in the get array are added to the symbol table.

Treat_data is a member of the Sapi_module_struct:

___fckpd___5

Now go back and look at Treat_data (i.e. Php_default_treat_data):

___fckpd___6

In the above logic, the res is replicated to Query_string, and the SG (request_info) is a structure representing the current request information, where query_string is in Php_apache_request_ ctor by copying the args in the Apache REQEUST_REC structure.

For the example of this article, Res is now "Name=laruence&career[]=yahoo&career[]=baidu",

To continue in the Treat_data, the following logic is:

___fckpd___7

First, by php_strtok_r the res into a "Key=value" segment based on "&", then the Var and Val are copied as key and value respectively, noting that the Var and Val are php_url_ in this process. Decode.

Finally, add a member named Var value of Php_register_variable_safe to Array_ptr (http_globals) [Track_vars_get], or $_get, at this point.

In this step, our $_get array contains the following members:

___fckpd___8

To be continued (the destruction process of the variable) ...



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.