PHP extension development Entry 3 & gt; the original notes of the function with parameters are the same as those of the development code. Later, I saw more code and blogs. The discovery is too casual, it is difficult to extract high-quality code, and it is difficult to write notes that can be understood.
Note-taking is a process of organizing knowledge and expressing your familiarity with this knowledge.
Today, I wrote a piece of code to record how to receive parameters in the PHP extension.
I will not talk much about it. as for the basics, for example, how to create an extension is not mentioned here. to write a function that receives parameters, you only need to modify the function. The functions written yesterday are as follows:
/* Here is the function. This function does not receive parameters, but only returns values. */PHP_FUNCTION (helloworld) {int len; char * strg; len = spprintf (& strg, 0, "%. 78 s "," helloworld "); RETURN_STRINGL (strg, len, 0 );}
Here, the value of a + B is calculated for the receiving parameter. First, add the following code in the method to receive parameters:
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) { return; }
To receive parameters, you need to understand the following content. to receive an integer, you need to write an 'l'. to receive two parameters, you need to write two 'L', and so on ,, then, after the receiving parameter is written, the value is assigned to the specified variable address. one parameter writes one variable, two writes, and three writes, and so on. For strings and other parameters, return the same description.
B Booleanl Integer d Floating point s String r Resource a Array o Object instance Object O Object instance of a specified type specific type Object z Non-specific zval any type ~ Z zval ** type f indicates the function and method name. in PHP5.1, it looks like the wood ....
The modified code is as follows:
/* Here is the function. This function does not receive parameters, but only returns values. */PHP_FUNCTION (helloworld) {int a; int B; if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "ll", & a, & B) = FAILURE) {return ;} int c; c = a + B; RETURN_LONG (c );}
In this way, a function for addition calculation is complete. Next, compile and start. In the PHP code, call the following code: