PHP extension development-07-function call

Source: Internet
Author: User
1. the return value of the function has finally finished zval. previously we talked about the definition and use of the function, but we didn't talk about the return value of the function. In C extension, the return value type of the function is zval, so we will explain this section here. In this section, we will write a simple example... "/> <scripttype =" text/javascript "src =" http://www.2 1. the return value of the function has finally finished zval. previously we talked about the definition and use of the function, but we didn't talk about the return value of the function. In C extension, the return value type of the function is zval, so we will explain this section here. In this section, we will write a simple calculator to complete the addition, subtraction, multiplication, division operation. the requirements are as follows: Compile The calculate (num1, num2, opt) function. we want to complete num1 opt num2 =?. Let's take a look at the code to complete the above task: PHP_FUNCTION (calculate) {int num1; int num2; char * opt; int opt_len; if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "lls ", & num1, & num2, & opt, & opt_len) = FAILURE) {php_printf ("param error. example: calculate (123,456, '+') \ n "); RETURN_NULL ();} if (1! = Opt_len) {php_printf ("param error. example: calculate (123,456, '+') \ n "); RETURN_NULL ();} switch (opt [0]) {case '+': return_value-> type = IS_LONG; return_value-> value. lval = num1 + num2; break; case '-': return_value-> type = IS_LONG; return_value-> value. lval = num1-num2; break; case '*': return_value-> type = IS_LONG; return_value-> value. lval = num1 * num2; break; case '/': return_value-> type = IS_DOUBLE; return_value-> value. lval = num1 * 1.0/num2; break; default: return_value-> type = IS_LONG; return_value-> value. lval = 0; break;} I saw the above code. do you have any questions about how return_value came from? Return_value is defined in your macro PHP_FUNCTION macro. PHP_FUNCTION declares this variable and assigns this variable to NULL. let's take a look at the macro definition: # define ZEND_NAMED_FUNCTION (name) void name (INTERNAL_FUNCTION_PARAMETERS) # define ZEND_FUNCTION (name) evaluate (ZEND_FN (name) # define tune ht, zval * return_value, \ zval ** return_value_ptr, zval * this_ptr, and limit TSRMLS_DC are defined in the macro above, return_value is a zval pointer. php uses this variable to point to the return value of the function.. In fact, we also have a macro that can be simply returned. the macro is defined as follows: RETVAL _ * (v), * represents various variable types, and v represents the value of the variable, for example, RETVAL_LONG (34) returns a value of the long type, whose value is 34.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.