Because I infer that the memory address space of null has no value, while the other two have addresses
PHPz2017-05-16 13:19:02
The following arguments are all based on
PHP 5
Structure changes according to php 5.6
的zval
类型(PHP 7
)
typedef struct _zval_struct zval;
...
struct _zval_struct {
/* Variable information */
zvalue_value value; // 值
zend_uint refcount__gc; // 引用计数,默认 1
zend_uchar type; // 变量类型
zend_uchar is_ref__gc; // 是否是引用,默认 0
};
The value of zend_uchar type
的type
can be
IS_NULL
IS_BOOL
IS_LONG
IS_DOUBLE
IS_STRING
IS_ARRAY
IS_OBJECT
IS_RESOURCE
PHP distinguishes the types of variables based on type
Sonull
对php
来说,与bool
/long
comparison, there is no special advantage
null
变量,表示zvalue_value value
无需赋值,相比String
、Array
需要申请大量内存的操作来说,还是具备一定的优势,但是对比LONG
、BOOL
In terms of performance, this performance advantage can basically be ignored. The explanation is as follows:
is as small as a bool value and as large as a composite array, in php
核心实现中,都是C语言的zVal结构
.
To sum it up, it is the following PHP statement:
$n = null;
$a = true;
$b = 123;
$c = 321.123;
$d = 'string';
$e = [];
$f = new SomeClass();
$g = fopen('xxx', 'r');
$h = &$b; //引用,比较特殊
The above variables, when /usr/bin/php
is executed to this line, will be converted into zVal structure
in the memory. The value of /usr/bin/php
执行到这行时,都会在内存中转化为zVal结构
,type
has been explained above and will be explained below. How its value will be stored,
zvalue_value value
The value of the PHP variable is stored in the zvalue_value value
变量中,其中zvalue_value
variable, where the structure of zvalue_value
is as follows:
typedef union _zvalue_value {
long lval; //整形
double dval; //浮点
struct { //字符串
char *val;
int len;
} str;
HashTable *ht; //数组, 也就是hashmap
zend_object_value obj; // Object
} zvalue_value;
union
在C语言里面是一个联合体
means that only one member will take effect at a time. Its characteristic is that the length of memory=length of the longest member.
The members used by PHP variables are as follows:
NULL 不使用
BOOL/LONG 使用 lval;
DOUBLE 使用 dval;
String 使用 str;
Array 使用 ht;
Resource 使用 lval;
Object 使用 obj;
The implementation of
Object
、Resource
、Array
will be very complicated and is not discussed in this article. For details, please check the hyperlink at the bottom of the text.
According to the above table, after setting type = IS_NULL
to a variable of type NULL
, there is no need to assign value
NULL
类型的变量在设置type = IS_NULL
之后,而无需赋值value
而0/false
赋值在设置type = IS_BOOL / IS_LONG
之后,再多一句赋值value.lval = 0;
and 0/false< /code>Assignment After setting
type = IS_BOOL / IS_LONG
, add one more assignment value.lval = 0;
:
However, no matter whether the member
in value
is assigned a value or not, value
中的成员
是否赋值,zvalue_value value
it needs to occupy memory ,
This is the process of NULL
仅仅是少一个赋值 4 bytes
memory, but from the perspective of modern CPUs, this advantage can be ignored.
Please see http://www.php-internals.com/...
for details