Is null more efficient than false and 0 in php?
伊谢尔伦
伊谢尔伦 2017-05-16 13:17:02
0
2
487

Because I infer that the memory address space of null has no value, while the other two have addresses

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
PHPzhong

The following arguments are all based on PHP 5

Structure changes according to php 5.6zval类型(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 typetype 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

Conclusion

Sonullphp来说,与bool/longcomparison, there is no special advantage

No advantage

null变量,表示zvalue_value value无需赋值,相比StringArray需要申请大量内存的操作来说,还是具备一定的优势,但是对比LONGBOOLIn 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:

$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,

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:

NULL      不使用
BOOL/LONG 使用    lval;
DOUBLE    使用    dval;
String    使用    str;
Array     使用    ht;
Resource  使用    lval;
Object    使用    obj;

The implementation of ObjectResourceArray 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.

Please see http://www.php-internals.com/...

for details
世界只因有你

It can be ignored in actual projects

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template