Thoroughly understand PHP Variable Structures and php Variable Structures

Source: Internet
Author: User

Thoroughly understand PHP Variable Structures and php Variable Structures

Zval in PHP5

// 1. zvaltypedef struct _ zval_struct {zvalue_value value; zend_uint refcount _ gc; zend_uchar type; zend_uchar is_ref _ gc;} zval; // 2. zvalue_valuetypedef union _ zvalue_value {long lval; // used for bool type, integer type, and Resource Type double dval; // used for floating point type struct {// used for string char * val; int len ;} str; HashTable * ht; // used for Array zend_object_value obj; // used for object zend_ast * ast; // used for constant expressions (only available in PHP5.6)} zvalue_value; // 3. zend_object_valuetypedef struct _ zend_object_value {zend_object_handle handle; const zend_object_handlers * handlers;} zend_object_value; // 4. zend_object_handletypedef unsigned int zend_object_handle;

Most of the articles mentioned the structure of the PHP5 variable: sizeof (zval) = 24, sizeof (zvalue_value) = 16. In fact, this discussion is not accurate, this result is correct when the CPU is 64bit.

But when the CPU is 32bit: sizeof (zval) = 16, sizeof (zvalue_value) = 8, mainly because when the CPU is 64bit, the pointer occupies 8 bytes, while 32 bit, the pointer is 4 bytes.

Zval in PHP 7

// 1. zvalstruct _zval_struct { zend_value  value;   /* value */ union {  struct {   ZEND_ENDIAN_LOHI_4(    zend_uchar type,   /* active type */    zend_uchar type_flags,    zend_uchar const_flags,    zend_uchar reserved)  /* call info for EX(This) */  } v;  uint32_t type_info; } u1; union {  uint32_t  next;     /* hash collision chain */  uint32_t  cache_slot;   /* literal cache slot */  uint32_t  lineno;    /* line number (for ast nodes) */  uint32_t  num_args;    /* arguments number for EX(This) */  uint32_t  fe_pos;    /* foreach position */  uint32_t  fe_iter_idx;   /* foreach iterator index */  uint32_t  access_flags;   /* class constant access flags */  uint32_t  property_guard;  /* single property guard */ } u2;};// 2. zend_valuetypedef union _zend_value { zend_long   lval;    /* long value */ double   dval;    /* double value */ zend_refcounted *counted; zend_string  *str; zend_array  *arr; zend_object  *obj; zend_resource *res; zend_reference *ref; zend_ast_ref  *ast; zval    *zv; void    *ptr; zend_class_entry *ce; zend_function *func; struct {  uint32_t w1;  uint32_t w2; } ww;} zend_value;

PHP 7 seems a lot more, but it is actually simpler. Whether the CPU is 32bit or 64bit, sizeof (zval) is always equal to 16.
The ww in zend_value is two uint32_t, which is always eight bytes. Therefore, sizeof (zend_value) = 8, so sizeof (zval) = 16.

Therefore, the memory saving mentioned in the new features of PHP7 is not changed in the 32bit system.

By the way, sizeof cannot be used as a function. Although it is written as a function, this value will be determined during the compilation period, rather than during the runtime. Similar to compile preprocessing.

Summary

The above is the PHP variable structure introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message. The editor will reply to you in time. Thank you very much for your support for the help House website!

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.