In PHP, the return value is the result value taken out from inside the function. The return value is returned by using the optional return statement. It can return any type including arrays and objects. The return statement will Immediately aborts the function's execution and returns control to the line of code that called the function.
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
What is the return value of the php function? In PHP, the return value of a function can be any type of data; of course, the function does not need to return a value. The function uses the return keyword to return data. When the function encounters the return keyword, execution will be terminated immediately.
The sample code is as follows:
function square($num){ return $num * $num; } echo square(4); //outputs'16'. ?>
What is the return value of the php function? The running result of the above code is:
16
The function can only have one return value
The function cannot return multiple values, but it can be returned by an array to get a similar effect. The code is as follows:
The code execution result is:
function small_numbers(){ return array(0, 1, 2); } list($zero, $one, $two) = small_numbers(); echo $zero . $one . $two; ?>
012
$zero $one $two The values are 0, 1, and 2 respectively.
Return value type declaration
What is the return value of the PHP function? In PHP 7, functions have added type declarations for return values. Similar to parameter type declaration, in non-strict mode, PHP will try to convert the return value type to the expected value type, but in strict mode, the return value of the function must be consistent with the declared return type.
The example is as follows:
function sum($a, $b):float{ return $a + $b; } var_dump( sum(1,2) ); ?>
The above program will output:
float(3)
The code in strict mode is as follows:
declare(strict_types=1); function sum($a, $b):int{ return $a + $b; } var_dump( sum(1,2) ); var_dump( sum(1,2.1) ); ?>
The value is returned by using the optional statement returns. Any type including arrays and objects can be returned. A return statement immediately aborts the function and returns control to the line of code that called the function.
A function is a collection of functions. Perform certain functions or operations. . It is meaningless to keep the result after the operation inside the function. The return value is to take the result of the function operation out of the function. Whether it is a custom function or a built-in function. The prototypes are as follows:
function 函数名(参数1, 参数2……) { 运算过程 return 运算结果; }
The function of the return keyword is to move the result of the operation out of the function. To get this value. You can use the = sign to assign a value to a variable.
$var = 函数名(参数,如果有的话);
$var can get the operation result inside the function. There are also some functions that have no return value. . For example, pay special attention to var_dump
. return value. It just returns the "value" of the operation result, not a specific variable. For example:
funciton test() { $a = 1+2; return $a; }
The above code returns the value 3. rather than $a itself. That’s why it’s called Return “value”
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does return value mean in php. For more information, please follow other related articles on the PHP Chinese website!