Home >Backend Development >PHP Problem >What is the usage of php return

What is the usage of php return

藏色散人
藏色散人Original
2021-03-08 09:52:292384browse

php return usage: 1. Return an expression result through the "return expression" syntax; 2. Use the "return(expr)" syntax to return a function expression; 3. Use return to return the value directly.

What is the usage of php return

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

Basic usage:

a), return expression // Return an expression result

b), return(expr) // Function expression Formula

c), return // Return directly, or return a null value

Note: It is best not to use return($val).

1. If return is executed, the content after the return statement will not be executed;

function add($a,$b){
 return $a+$b;
 return $a*$b;
}

$c = add(5,3);//The obtained $c value Can be used elsewhere in the program.

echo $c;

Output result: 8, only $a $b is executed, $a*$b is not executed.

[Recommended: "PHP Video Tutorial"]

2. Return can be a function return value or a null value, depending on the specific usage, for example :

function test($a){
if($a>10){
 return "a>10";
}else{
 return "a<10";
}
$b=45;
$c=$b-$a;
echo $c;
}

In this example, when you call this function and give it any number, it will return a string, and the code:

$b=45;
$c=$b-$a;
echo $c;

will never be implement.

The above is the detailed content of What is the usage of php return. 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