Home  >  Article  >  Backend Development  >  How to use return in php

How to use return in php

青灯夜游
青灯夜游Original
2021-07-09 18:01:402348browse

In PHP, return is used in functions to return function values ​​and terminate function execution; functions use the return keyword to return data, and when the function encounters the return keyword, it will immediately terminate execution. A function can only have one return value, but you can have the effect of returning multiple values ​​by returning an array.

How to use return in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, the return value of the function can be Any type of data; of course, functions can also return no values. The function uses the return keyword to return data. When the function encounters the return keyword, execution will be terminated immediately.

The return statement has two functions in the function definition:

  • One is to return the function value;

  • The second is Abort the execution of the function.

The sample code is as follows:

<?php
function square($num){
    return $num * $num;
}
echo square(4);  //outputs&#39;16&#39;.
?>

The running result of the above code is:

16

Function can only have one return value

The function cannot return multiple values, but you can get similar effects by returning an array. The code is as follows:

<?php
function small_numbers(){
    return array(0, 1, 2);
}
list($zero, $one, $two) = small_numbers();
echo $zero . $one . $two;
?>

The code execution result is:

012

$zero $one $two The values ​​are 0, 1, and 2 respectively.

Return value type declaration

In PHP 7, the function adds a return value type declaration. 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:

<?php
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:

<?php
declare(strict_types=1);
function sum($a, $b):int{
    return $a + $b;
}
var_dump( sum(1,2) );
var_dump( sum(1,2.1) );
?>

The execution result of the above program is:

int(3)
Fatal error: Uncaught TypeError: Return value of sum() must be of the type integer, float returned in /Library/WebServer/Documents/book/str.php:281 Stack trace: #0 /Library/WebServer/Documents/book/str.php(284): sum(1, 2.1) #1 {main} thrown in /Library/WebServer/Documents/book/str.php on line 281

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use return in php. 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