The composition and type of PHP variables

Source: Internet
Author: User
Tags php language key string
This article mainly introduces the composition and types of PHP variables, interested in the reference of friends, I hope to be helpful to everyone.

PHP Variable components:

Variable name: The PHP language variable name starts with $ + English/Underscore, can contain numbers, underscores, letters, and is case sensitive. PHP also supports compound variables, such as $ $A, which increases the dynamic nature of PHP.

Type: PHP is a weakly typed language and can be assigned any type of value.

Content: There can be only one value at a time.

There are 8 data types in the PHP language, divided into three main categories:

1. Scalar type: boolean,integer,float,string;

2. Compound type: object,array;

3. Special type: Null,resource;

PHP, as a weakly typed language, is the core of PHP's weak type to implement all internal variables that store data through structural zval, including not only the value of the variable, but also the type of the variable.

ZVAL Data structure:

struct _zval_struct{  zvalue_value value;    The value of the stored variable  zend_unint  refcount_gc;//reference count  Zend_char  is_ref_gc;  Whether to reference  Zend_char  type;     Type of storage variable}

Where Zvalue_value is not a struct, it is implemented in order to save the Union of memory use, because a variable can only represent one type at a time. Its prototype:

typedef Union _zvalue_value{  long lval;           Double dval;  struct {      char *val;      int Len;      The length of the string    }str;  HashTable *ht;       Save array  zend_object_value obj;   Object}zvalue_value;

Hash table:

Many implementations within PHP are based on a hash table: the scope of the variable, the function table, the properties of the class, the method, and so on, and much of the data inside the Zend engine is stored in the Hashtable.

The PHP array uses a Hashtable to store the associated data, and the hash table implementation uses two data structures hashtable and buckets:

HashTable:

typedef struct _HASHTABLE {   uint ntablesize;    The size of the hash bucket, with a minimum of 8, increases by 2x.  UINT Ntablemask;    NTableSize-1, optimization of index value  uint nnumofelements;  The number of elements currently present in the hash bucket, and the count () function will return this value directly to   ulong nnextfreeelement;//position of the next numeric index  Bucket *pinternalpointer;  The currently traversed pointer (one of the causes of foreach is faster than for fast)  Bucket *plisthead;     Storage array head element pointer  bucket *plisttail;     Storage array tail element pointer  bucket **arbuckets;     Store hash array  dtor_func_t pdestructor;  A callback function that executes when an element is deleted, and is used to release  zend_bool persistent of resources;    The method of allocating bucket memory is pointed out. If Persisient is true, the                  bucket is allocated memory using the memory allocation function of the operating system itself, otherwise                  PHP's memory allocation function is used.  unsigned char napplycount;//marks the number of times the current hash bucket has been accessed recursively (prevents multiple recursion)  Zend_bool bapplyprotection;// Mark the current hash bucket allows multiple accesses not allowed, when not allowed, up to 3 times can be recursive # if zend_debug  int inconsistent; #endif} HashTable;

In the Hashtable, the expansion of the capacity is always adjusted to approximate the initial size of 2 of the entire number of parties. Because:

When selecting a slot, this is done using & instead of modulo, because it is relatively expensive to take the modulo operation and the bitwise AND operation is much larger. Mask is the function of mapping the hash value to the range of indexes that the slot can store. For example: The index value of a key is 21, the hash table size is 8, then mask is 7, then the binary representation of the time is: 10101 & 111 = 101 is the decimal 5. Because 2 of the integer-1 of the binary is special: the value of the next n-bit is 1, so it is easier to map the value, if it is the normal number of binary and after the result will affect the hash value. Then the average distribution of the values computed by the hash function may be affected.

Bucket

typedef struct BUCKET {  ulong H;      The value of the hash after the Char *key, or the user-specified numeric index value  uint Nkeylength;  The length of the hash keyword, if the array index is a number, this value is 0  void *pdata;    Point to value, which is typically a copy of the user's data, or, if it is pointer data, to pdataptr  void *pdataptr;   In the case of pointer data, this value points to the true value, and the above pdata points to the value of the  struct bucket *plistnext;  The next element of the entire hash table is a  struct bucket *plistlast;  The entire hash table of the element's previous element,  struct bucket *pnext;    The next element in the same hash bucket is the  struct bucket *plast;    The previous element of the same hash bucket//holds the key string for the current value, this field can only be defined at the end, implementing the variable-length struct  char arkey[1];       } Buckets;

A hash value is stored in the bucket instead of the hash index.

The last field of the structure above is used to hold the string of key, but this field is declared as an array of only one character, in fact, this is a long-seeing variable-length structure, the main purpose is to increase flexibility. The following is the code for requesting space when inserting a new element into a Hashtable

p = (bucket *) pemalloc (sizeof (bucket)-1 + nkeylength, ht->persistent); if (!p) {  return FAILURE;} memcpy (P->arkey, Arkey, nkeylength);

Insert Process Diagram

Hashing algorithm

The hash function in PHP is implemented using the DJBX33A algorithm.

Object:

PHP objects use data structure zend_object_value to store;

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.