Home > Backend Development > PHP Tutorial > PHP variable principle

PHP variable principle

WBOY
Release: 2016-07-29 08:57:44
Original
810 people have browsed it

1.php as a weakly typed language does not need to explicitly indicate the type of variables, but php variables also have types. php variables include the following 8 types of variables (three major categories)

 a. Scalar type: boolean, integer, float (double), string

b. Composite type: array, object

c. Special type: resource, null

2.php uses c language to implement the principle of variables

a. Variable storage structure

typedef struct _zval_struct zval;
...
struct _zval_struct {
    /* Variable information */
    zvalue_value value;     /*存储变量的值,是一个union类型*/
    zend_uint refcount__gc;/*变量的引用计数,默认为1*/
    zend_uchar type;    /*变量的类型,为IS_NULL、IS_BOOL、IS_LONG、IS_DOUBLE、IS_STRING、IS_ARRAY、IS_OBJECT和IS_RESOURCE之一*/
    zend_uchar is_ref__gc;/*表示是否为引用*/
};
Copy after login

 b. The value of the stored variable zvalue_value is as follows

typedef union _zvalue_value {
    long lval;                  /* long value */
    double dval;                /* double value */
    struct {
        char *val;
        int len;
    } str;
    HashTable *ht;              /* hash table value */
    zend_object_value obj;
} zvalue_value;
Copy after login

The purpose of using union instead of struct here is to save memory space, thinking that a variable can only represent one type at the same time

Reference: tipi open source project http://www .php-internals.com/book/?p=chapt03/03-01-00-variables-structure

The above introduces the principle of PHP variables, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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