The pval/zval Data Type

Throughout the PHP source code, you will see references to both pval and zval . They are the same thing and can be used interchangeably. The pval/zval is the basic data container in PHP. All data that is passed between the extension API and the user-level script is passed in this container. You can dig into the header files further yourself, but in simple terms, this container is a union that can hold either a long, a double, a string including the string length, an array, or an object. The union looks like this:

typedef union _zvalue_value {
        long lval; 
        double dval;
        struct {
                char *val;
                int len;
        } str;
        HashTable *ht;
        zend_object obj;
} zvalue_value;

The main things to learn from this union are that all integers are stored as longs, all floating-point values are stored in double-precision, and every string has an associated string length value, which, if properly checked everywhere, makes strings in PHP binary-safe.[9] Strings do not need to be null-terminated, but since most third-party libraries expect null-terminated strings it is a good idea to always null-terminate any string you create.

Along with this union, each container has a flag that holds the currently active type, whether it is a reference or not, and the reference count. So the actual pval /zval struct looks like this:

struct _zval_struct {
        zvalue_value value;
        zend_uchar type;
        zend_uchar is_ref;
        zend_ushort refcount;
};

Because this structure could change in future versions of PHP, be sure ...

Get Programming PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.