Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to pass the participation return value of PHP7

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to transmit the participation return value of PHP7. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Preface

This time, we will demonstrate how to accept incoming parameters and output return values in the PHP extension.

We will implement the default_value method in the extension. []

The code implements the default_value method

The PHP extension source code for the default_value method:

PHP_FUNCTION (default_value) {zend_string * type; zval * value = NULL; # ifndef FAST_ZPP / * Get function parameters and do error-checking. * / if (zend_parse_parameters (ZEND_NUM_ARGS (), "S | z", & type, & value) = = FAILURE) {return;} # else ZEND_PARSE_PARAMETERS_START (1,2) Z_PARAM_STR (type) Z_PARAM_OPTIONAL Z_PARAM_ZVAL_EX (value, 0,1) ZEND_PARSE_PARAMETERS_END () # endif if (ZSTR_LEN (type) = = 3 & & strncmp (ZSTR_VAL (type), "int", 3) = = 0 & & value = = NULL) {RETURN_LONG (0);} else if (ZSTR_LEN (type) = = 3 & & strncmp (ZSTR_VAL (type), "int", 3) = 0 & value! = NULL) {RETURN_ZVAL (value, 0,1) } else if (ZSTR_LEN (type) = = 4 & & strncmp (ZSTR_VAL (type), "bool", 4) = = 0 & & value = = NULL) {RETURN_FALSE;} else if (ZSTR_LEN (type) = = 4 & & strncmp (ZSTR_VAL (type), "bool", 4) = = 0 & value! = NULL) {RETURN_ZVAL (value, 0,1) } else if (ZSTR_LEN (type) = = 3 & & strncmp (ZSTR_VAL (type), "str", 3) = = 0 & & value = = NULL) {RETURN_EMPTY_STRING ();} else if (ZSTR_LEN (type) = = 3 & & strncmp (ZSTR_VAL (type), "str", 3) = = 0 & value! = NULL) {RETURN_ZVAL (value, 0,1);} RETURN_NULL () } Code description to get parameters

There are two ways to get parameters in PHP7. Zend_parse_parameters and FAST ZPP.

Zend_parse_parameters

Until PHP7, you used the zend_parse_parameters function to get parameters. The function of this function is to convert the passed parameters to the corresponding type in the PHP kernel, which is convenient for use in PHP extensions.

Parameter description:

The first parameter, the number of parameters. ZEND_NUM_ARGS () is generally used and does not need to be changed.

The second parameter, formatting the string. The purpose of this formatted string is to specify the conversion relationship between the incoming parameters and the PHP kernel type.

S | z in the code means:

S indicates that the parameter is a string. To convert the passed-in parameters to zend_string types.

| indicates that the subsequent parameters are optional. You can pass it or not.

Z indicates that the parameters are of multiple types. To convert the passed-in parameters to zval types.

In addition, there are some specifier that need to be noted:

! If you receive a null variable in the PHP language, convert it directly to a NULL in the C language, rather than a zval encapsulated as an IS_NULL type.

/ if the passed variable shares a zval with other variables and is not a reference, a forced separation is performed, the new zval is_ref__gc==0, and refcount__gc==1.

For more information on the meaning of the formatted string, see the official website. Https://wiki.php.net/rfc/fast_zpp

FAST ZPP

The new way provided in PHP7. Is to improve the performance of parameter parsing. For frequently used methods, it is recommended to use FAST ZPP.

Mode of use:

Start with ZEND_PARSE_PARAMETERS_START (1, 2).

The first parameter represents the number of parameters that must be passed, and the second parameter represents the maximum number of parameters passed in.

End with ZEND_PARSE_PARAMETERS_END ();.

In the middle is the parsing of the incoming parameters.

It is worth noting that the general macro method of FAST ZPP corresponds to the specifier of zend_parse_parameters. Such as:

Z_PARAM_OPTIONAL corresponds to |

Z_PARAM_STR corresponds to S

However, the Z_PARAM_ZVAL_EX method is special. It corresponds to two specifier, one is! And /. ! Corresponds to the second parameter of the macro method. / corresponds to the third parameter of the macro method. If you want to turn it on, just set it to 1.

For the corresponding macro method of FAST ZPP, you can check the official website https://wiki.php.net/rfc/fast_zpp#proposal

Return value

The return value of the method is returned using a macro method that begins with RETURN_. Common macro methods are:

RETURN_NULL () returns null

RETURN_LONG (l) returns an integer

RETURN_DOUBLE (d) returns a floating point type

RETURN_STR (s) returns a string. Parameter is a zend_string * pointer

RETURN_STRING (s) returns a string. Parameter is a char * pointer

RETURN_STRINGL (s, l) returns a string. The second parameter is the string length.

RETURN_EMPTY_STRING () returns an empty string.

RETURN_ARR (r) returns an array. The parameter is a zend_array * pointer.

RETURN_OBJ (r) returns an object. The parameter is a zend_object * pointer.

RETURN_ZVAL (zv, copy, dtor) returns any type. The parameter is a zval * pointer.

RETURN_FALSE returns false

RETURN_TRUE returns true

This is the end of the article on "how to send participation return values for PHP7". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report