Let’s answer the question in the previous section first.
Global is used to define global variables, but this global variable does not apply to the entire website, but to the current page, including all files in include or require.
The three basic characteristics of variables are mentioned in the preface. One of them is the type of variables. Variables have specific types, such as strings, arrays, objects, etc. The type system of programming languages can be divided into two types: strong type and weak type:
Strongly typed language means that once a variable is declared as a variable of a certain type, the type of the variable cannot be changed during the running of the program. Values other than other values are assigned to it (of course, this is not entirely the case. This may involve type conversion, which will be introduced in the following sections). Languages such as C/C++/Java fall into this category.
Script languages such as PHP, Ruby, and JavaScript are weakly typed languages: a variable can represent any data type.
A large part of the reason why PHP has become a simple and powerful language is that it has weakly typed variables. But sometimes this is also a double-edged sword, and improper use can cause some problems. Just like an instrument, the more powerful it is, the greater the potential for error.
In the official PHP implementation, all variables are stored using the same data structure (zval), and this structure also represents various data types in PHP. It contains not only the value of the variable, but also the type of the variable. This is the core of PHP's weak typing.
So how does the zval structure implement weak types? Let’s uncover the veil together.
Variable storage structure
PHP does not need to explicitly indicate its data type when declaring or using variables.
PHP is a weakly typed language, which does not mean that PHP has no types. In PHP, there are 8 variable types, which can be divided into three categories
* Scalar types: boolean, integer, float (double), string
* Composite types: array, object
* Special types: resource, NULL
Official PHP is implemented in C, and C is a strongly typed language, so how does this implement weak typing in PHP? ?
The value of the variable is stored in the zval structure shown below. The zval structure is defined in the Zend/zend.h file, and its structure is as follows:
PHP uses this structure to store all data for variables. Unlike other compiled static languages, PHP also saves the PHP user space variable type in the same structure when storing variables. In this way we can obtain the type of the variable through this information.
There are four fields in the zval structure, their meanings are:
属性名 | 含义 | 默认值 |
refcount__gc | 表示引用计数 | 1 |
is_ref__gc | 表示是否为引用 | 0 |
value | 存储变量的值 | |
type | 变量具体的类型 |
这里使用联合体而不是用结构体是出于空间利用率的考虑,因为一个变量同时只能属于一种类型。 如果使用结构体的话将会不必要的浪费空间,而PHP中的所有逻辑都围绕变量来进行的,这样的话, 内存浪费将是十分大的。这种做法成本小但收益非常大。
各种类型的数据会使用不同的方法来进行变量值的存储,其对应赋值方式如下:
1. 一般类型
Variable type | Macro | ? |
boolean | ZVAL_BOOL | Boolean/integer variable values are stored in (zval).value.lval, and their types will also be stored with the corresponding IS_*. Z_TYPE_P(z)=IS_BOOL/LONG; Z_LVAL_P(z)=((b)!=0); |
integer | ZVAL_LONG | |
float | ZVAL_DOUBLE | |
null | ZVAL_NULL | NULL值的变量值不需要存储,只需要把(zval).type标为IS_NULL。 Z_TYPE_P(z)=IS_NULL; |
resource | ZVAL_RESOURCE | 资源类型的存储与其他一般变量无异,但其初始化及存取实现则不同。 Z_TYPE_P(z) = IS_RESOURCE; Z_LVAL_P(z) = l; |
The character string in C starts with
3. ArrayArray is the most commonly used and most powerful variable type in PHP. It can store other types of data and provides various built-in operation functions. The storage of arrays is more complicated than other variables. The value of the array is stored in the zvalue_value.ht field, which is a HashTable type data. PHP's arrays use hash tables to store associated data. A hash table is an efficient key-value pair storage structure. Two data structures, HashTable and Bucket, are used in PHP's hash table implementation. All the work of PHP is implemented by hash tables. In the next section HashTable, we will introduce the basic concepts of hash tables and the implementation of hash tables in PHP.
4. Object
In object-oriented languages, we can define the data types we need, including class attributes, methods and other data. An object is a specific implementation of a class. Objects have their own state and operations they can complete.
PHP’s object is a composite data, which is stored using a zend_object_value structure. Its definition is as follows:
PHP’s weak variable container is implemented in an inclusive form, and each type of variable has its corresponding tag and storage space. Languages that use strong types are usually more efficient than weak types because a lot of information can be determined before running, which can also help eliminate program errors. The problem this brings is that writing code is relatively restricted.