PHP distinguishes the types of variables based on type
Conclusion
Sonull对php来说,与bool/longcomparison, there is no special advantage
No advantage
null变量,表示zvalue_value value无需赋值,相比String、Array需要申请大量内存的操作来说,还是具备一定的优势,但是对比LONG、BOOLIn terms of performance, this performance advantage can basically be ignored. The explanation is as follows:
What is
zVal
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:
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,
The structure of 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:
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 valueNULL类型的变量在设置type = IS_NULL之后,而无需赋值value 而0/false赋值在设置type = IS_BOOL / IS_LONG之后,再多一句赋值value.lval = 0;and 0/falseAssignment 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 bytesmemory, but from the perspective of modern CPUs, this advantage can be ignored.
Structure changes according to
The value ofphp 5.6
的zval
类型(PHP 7
)zend_uchar type
的type
can bePHP distinguishes the types of variables based on
type
Conclusion
So
null
对php
来说,与bool
/long
comparison, there is no special advantageNo advantage
What isnull
变量,表示zvalue_value value
无需赋值,相比String
、Array
需要申请大量内存的操作来说,还是具备一定的优势,但是对比LONG
、BOOL
In terms of performance, this performance advantage can basically be ignored. The explanation is as follows:zVal
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:
The above variables, when
/usr/bin/php
is executed to this line, will be converted intozVal 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,The structure of
zvalue_value value
The value of the PHP variable is stored in the
zvalue_value value
变量中,其中zvalue_value
variable, where the structure ofzvalue_value
is as follows: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:
According to the above table, after setting
type = IS_NULL
to a variable of typeNULL
, there is no need to assignvalue
NULL
类型的变量在设置type = IS_NULL
之后,而无需赋值value
而
0/false
赋值在设置type = IS_BOOL / IS_LONG
之后,再多一句赋值value.lval = 0;
and0/falseAssignment After setting
type = IS_BOOL / IS_LONG
, add one more assignmentvalue.lval = 0;
:However, no matter whether the
member
invalue
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 detailsIt can be ignored in actual projects