Home > Article > Backend Development > PHP extension development - Confusion caused by two zend_api during PHP extension development
My extension code is like this
<code>ZEND_BEGIN_ARG_INFO_EX(helloZvalArg, 0, 0, 1) ZEND_ARG_INFO (0, name) ZEND_ARG_INFO (0, age) ZEND_ARG_INFO (0, zval_args) ZEND_END_ARG_INFO() PHP_FUNCTION(debug_zval) { zend_string *name, *age; zval zval_args; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SSz", &name, &age, &zval_args) == FAILURE){ php_error_docref(NULL TSRMLS_CC, E_WARNING, "参数接受错误"); RETURN_FALSE; } php_printf("zval_args的type类型:%d\n", Z_TYPE(zval_args)); ZVAL_STR(&zval_args, name); php_printf("将name复制后的zval_args的type类型:%d\n",Z_TYPE (zval_args)); php_printf("named:%s\n",name->val); php_printf("age:%s\n",age->val); zval_ptr_dtor(&zval_args); }</code>
PHP code
<code><?php $br = (php_sapi_name() == "cli")? "":"<br>"; if(!extension_loaded('helloZval')) { dl('helloZval.' . PHP_SHLIB_SUFFIX); } debug_zval("sss",2,4); ?> </code>
Output
<code>zval_args的type类型:0 (IS_UNDF) 将name复制后的zval_args的type类型:6(IS_STRING) named:sss age:2</code>
I have two doubts
When I use zval as a container for receiving parameters (let’s call it that), why do I not receive the parameters successfully, but IS_UNDEF
If zval cannot be used as a parameter container, then zend_string has other types that are defined at the same time (such as zend_array, zend_object....) What is the difference between zend_string and zval (both zend_string and zval can be GCed and zend_string is more lightweight) , or what is the difference between the usage of zval and zend_string?
My extension code is like this
<code>ZEND_BEGIN_ARG_INFO_EX(helloZvalArg, 0, 0, 1) ZEND_ARG_INFO (0, name) ZEND_ARG_INFO (0, age) ZEND_ARG_INFO (0, zval_args) ZEND_END_ARG_INFO() PHP_FUNCTION(debug_zval) { zend_string *name, *age; zval zval_args; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SSz", &name, &age, &zval_args) == FAILURE){ php_error_docref(NULL TSRMLS_CC, E_WARNING, "参数接受错误"); RETURN_FALSE; } php_printf("zval_args的type类型:%d\n", Z_TYPE(zval_args)); ZVAL_STR(&zval_args, name); php_printf("将name复制后的zval_args的type类型:%d\n",Z_TYPE (zval_args)); php_printf("named:%s\n",name->val); php_printf("age:%s\n",age->val); zval_ptr_dtor(&zval_args); }</code>
PHP code
<code><?php $br = (php_sapi_name() == "cli")? "":"<br>"; if(!extension_loaded('helloZval')) { dl('helloZval.' . PHP_SHLIB_SUFFIX); } debug_zval("sss",2,4); ?> </code>
Output
<code>zval_args的type类型:0 (IS_UNDF) 将name复制后的zval_args的type类型:6(IS_STRING) named:sss age:2</code>
I have two doubts
When I use zval as a container for receiving parameters (let’s call it that), why do I not receive the parameters successfully, but IS_UNDEF
If zval cannot be used as a parameter container, then zend_string has other types that are defined at the same time (such as zend_array, zend_object....) What is the difference between zend_string and zval (both zend_string and zval can be GCed and zend_string is more lightweight) , or what is the difference between the usage of zval and zend_string?