The pval /zval Data Type
Throughout the PHP source code, you will see references to
both pval
and zval
. pval
is the old name and is gradually being changed over to zval
in the PHP source code. They are the same
thing and can be used interchangeably, but zval
should be used in new code. 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 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_value 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.[*] Strings should be null-terminated even if they represent binary data; otherwise, the Zend Engine will complain. Since most third-party libraries expect null-terminated strings, this shouldn’t pose a problem.
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
structure looks like this:
struct _zval_struct ...
Get Programming PHP, 2nd Edition 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.