Home  >  Article  >  Backend Development  >  PHP return value return statement usage detailed explanation

PHP return value return statement usage detailed explanation

伊谢尔伦
伊谢尔伦Original
2017-06-27 09:32:264488browse

In Programming language, a function or a method generally returns a value, but there are also situations where it does not return a value. At this time, these functions only process some transactions and do not return, or in other words There is no explicit return value, it has a proprietary keyword procedure in Pascal language. In PHP, functions have return values, which can be divided into two situations: using a return statement to explicitly return and returning NULL without a return statement.

return statement

When using the return statement, PHP returns a variable of the specified type to the user-defined function. The same way we view the source code, after performing lexical analysis and syntax analysis on the return keyword, we generate intermediate code. From the Zend/zend_language_parser.y file, it can be confirmed that the intermediate code generated calls the zend_do_return function.

void zend_do_return(znode *expr, int do_end_vparse TSRMLS_DC) /* {{{ */{
 zend_op *opline;
   int start_op_number, end_op_number;     if (do_end_vparse) {
       if (CG(active_op_array)->return_reference                && !zend_is_function_or_method_call(expr)) {
           zend_do_end_variable_parse(expr, BP_VAR_W, 0 TSRMLS_CC);/* 处理返回引用 */
       } else {
           zend_do_end_variable_parse(expr, BP_VAR_R, 0 TSRMLS_CC);/* 处理常规变量返回 */
       }
   } 
  ...// 省略  取其它中间代码操作 
   opline->opcode = ZEND_RETURN;     if (expr) {
       opline->op1 = *expr;         if (do_end_vparse && zend_is_function_or_method_call(expr)) {
           opline->extended_value = ZEND_RETURNS_FUNCTION;
       }
   } else {
       opline->op1.op_type = IS_CONST;
       INIT_ZVAL(opline->op1.u.constant);
   } 
   SET_UNUSED(opline->op2);}/* }}} */

The generated intermediate code is ZEND_RETURN. When the return value is a usable expression, the type of the first operand is the operation type of the expression, otherwise the type is IS_CONST. This is useful when subsequent calculations execute intermediate code functions. Depending on the operands, the ZEND_RETURN intermediate code will execute ZEND_RETURN_SPEC_CONST_HANDLER, ZEND_RETURN_SPEC_TMP_HANDLER or ZEND_RETURN_SPEC_TMP_HANDLER. The execution flows of these three functions are basically similar, including the handling of some errors. Here we take ZEND_RETURN_SPEC_CONST_HANDLER as an example to illustrate the execution process of the function return value:

static int ZEND_FASTCALL  ZEND_RETURN_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS){
 zend_op *opline = EX(opline);
   zval *retval_ptr;
   zval **retval_ptr_ptr; 
     if (EG(active_op_array)->return_reference == ZEND_RETURN_REF) {         //  返回引用时不允许常量和临时变量
       if (IS_CONST == IS_CONST || IS_CONST == IS_TMP_VAR) {   
           /* Not supposed to happen, but we'll allow it */
           zend_error(E_NOTICE, "Only variable references \                should be returned by reference");
           goto return_by_value;
       } 
       retval_ptr_ptr = NULL;  //  返回值         if (IS_CONST == IS_VAR && !retval_ptr_ptr) {
           zend_error_noreturn(E_ERROR, "Cannot return string offsets by reference");
       }         if (IS_CONST == IS_VAR && !Z_ISREF_PP(retval_ptr_ptr)) {
           if (opline->extended_value == ZEND_RETURNS_FUNCTION &&
               EX_T(opline->op1.u.var).var.fcall_returned_reference) {
           } else if (EX_T(opline->op1.u.var).var.ptr_ptr ==
                   &EX_T(opline->op1.u.var).var.ptr) {
               if (IS_CONST == IS_VAR && !0) {
                     /* undo the effect of get_zval_ptr_ptr() */
                   PZVAL_LOCK(*retval_ptr_ptr);
               }
               zend_error(E_NOTICE, "Only variable references \                 should be returned by reference");
               goto return_by_value;
           }
       }         if (EG(return_value_ptr_ptr)) { //  返回引用
           SEPARATE_ZVAL_TO_MAKE_IS_REF(retval_ptr_ptr);   //  is_refgc设置为1
           Z_ADDREF_PP(retval_ptr_ptr);    //  refcountgc计数加1             (*EG(return_value_ptr_ptr)) = (*retval_ptr_ptr);
       }
   } else {return_by_value: 
       retval_ptr = &opline->op1.u.constant;         if (!EG(return_value_ptr_ptr)) {
           if (IS_CONST == IS_TMP_VAR) {             }
       } else if (!0) { /* Not a temp var */
           if (IS_CONST == IS_CONST ||
               EG(active_op_array)->return_reference == ZEND_RETURN_REF ||
               (PZVAL_IS_REF(retval_ptr) && Z_REFCOUNT_P(retval_ptr) > 0)) {
               zval *ret; 
               ALLOC_ZVAL(ret);
               INIT_PZVAL_COPY(ret, retval_ptr);   //  复制一份给返回值 
               zval_copy_ctor(ret);
               *EG(return_value_ptr_ptr) = ret;
           } else {
               *EG(return_value_ptr_ptr) = retval_ptr; //  直接赋值
               Z_ADDREF_P(retval_ptr);
           }
       } else {
           zval *ret; 
           ALLOC_ZVAL(ret);
           INIT_PZVAL_COPY(ret, retval_ptr);    //  复制一份给返回值 
           *EG(return_value_ptr_ptr) = ret;    
       }
   }     return zend_leave_helper_SPEC(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);   //  返回前执行收尾工作}

The return value of the function is stored in *EG(return_value_ptr_ptr) when the program is executed. The ZE kernel distinguishes between value return and reference return, and on this basis, constants, temporary variables and other types of variables are treated differently when returned. Before the return is executed, the ZE kernel clears the variables used inside the function by calling the zend_leave_helper_SPEC function. This is one of the reasons why the ZE kernel automatically adds NULL returns to functions.

Function without return statement

In PHP, there is no concept of procedure, only functions without return value. But for functions that have no return value, the PHP kernel will "help you" add a NULL as the return value. This "helping you" operation is also performed when generating intermediate code. The function zend_do_end_function_declaration needs to be executed when parsing each function. There is a statement in this function:

zend_do_return(NULL, 0 TSRMLS_CC);

Combined with the previous content, we know that the function of this statement is to return NULL. This is why functions without a return statement return NULL.

The return value of the internal function is passed through a variable named return_value. This variable is also a parameter in the function, which can be seen after the PHP_FUNCTION function is expanded. This parameter always contains a zval container with pre-allocated space, so you can directly access its members and modify them without first executing the MAKE_STD_ZVAL macro on the return_value. In order to make it easier to return results from functions and save the trouble of directly accessing the internal structure of the zval container, ZEND provides a large set of macro commands to complete these related operations. These macros automatically set the type and value.

Macros that return values ​​directly from functions:

RETURN_RESOURCE(resource) returns a resource.

RETURN_BOOL(bool) Returns a Boolean value.

RETURN_NULL() returns a null value.

RETURN_LONG(long) Returns a long integer.

RETURN_DOUBLE(double) Returns a double-precision floating point number.

RETURN_STRING(string, duplicate) Returns a string. duplicate indicates whether this character is copied using estrdup().

RETURN_STRINGL(string, length, duplicate) returns a fixed-length string. The rest is the same as RETURN_STRING. This macro is faster and binary safe.

RETURN_EMPTY_STRING() returns an empty string.

RETURN_FALSE Returns a Boolean value false.

RETURN_TRUE Returns a boolean true value.

Macro for setting function return value:

RETVAL_RESOURCE(resource) Set the return value to a specified resource.

RETVAL_BOOL(bool) Set the return value to a specified Boolean value.

RETVAL_NULL Set the return value to a null value

RETVAL_LONG(long) Set the return value to a specified long integer.

RETVAL_DOUBLE(double) Set the return value to a specified double-precision floating point number.

RETVAL_STRING(string, duplicate) Set the return value to a specified string. The meaning of duplicate is the same as RETURN_STRING.

RETVAL_STRINGL(string, length, duplicate) Set the return value to a specified fixed-length string. The rest is the same as RETVAL_STRING. This macro is faster and binary safe.

RETVAL_EMPTY_STRING Set the return value to an empty string.

RETVAL_FALSE Set the return value to Boolean false.

RETVAL_TRUE Set the return value to Boolean true.

If you need to return complex types of data such as arrays and objects, you need to call array_init() and object_init() first, or you can use the corresponding hash function to directly operate return_value. Since these types are mainly composed of miscellaneous things, there are no corresponding macros for them.



The above is the detailed content of PHP return value return statement usage detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn