1 函数返回值
终于讲完zval了,前面我们讲到了函数的定义和使用,但是我们没有讲函数的返回值。因为C扩展中,函数的返回值类型为zval的,所以我们把这一节放在了这里进行讲解。
本节任务,我们写一个简单的计算器,完成加减乘除运算,要求:编写函数calculate(num1, num2, opt),我们希望完成num1 opt num2= ?的运算。我们看一下完成上面任务的代码:
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;
}
}
看到上面代码,不知道大家有没有疑惑,return_value是怎么来的?
return_value是你宏PHP_FUNCTION宏中定义的,PHP_FUNCTION会声明这个变量,并且将这个变量赋值为NULL,我们来看一下宏定义:
#define ZEND_NAMED_FUNCTION(name) void name(INTERNAL_FUNCTION_PARAMETERS)
#define ZEND_FUNCTION(name) ZEND_NAMED_FUNCTION(ZEND_FN(name))
#define INTERNAL_FUNCTION_PARAMETERSint ht, zval *return_value, \
zval **return_value_ptr, zval *this_ptr, intreturn_value_used TSRMLS_DC
从上面的宏定义来看,return_value就是zval的一个指针,php用这个变量来指向函数的返回值。其实我们也有一个宏可以进行简单的返回,宏定义如下,RETVAL_*(v),*表示的就是各种变量类型,v表示变量的值,例如RETVAL_LONG(34),将返回一个long类型的数值,其值为34。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
