In most programming languages, the return keyword can return the execution result of a function. The usage of return in PHP is also similar. For beginners, master the usage of return in PHP. It is also the beginning of learning PHP.
Recommended tutorial: PHP video tutorial
## First of all, it means return; return() is a language structure Instead of a function, parentheses are only required if the argument contains an expression. Parentheses are usually not used when returning a variable, which can reduce the burden on PHP. Basic usage:a), return expression // Return an expression result
b), return(expr) / / Function expression
c), return // Return directly, or return a null value
Note: It is best not to use return($val) way.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);//得到的$c值可以用在程序的其他地方! echo $c;
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; }
$b=45; $c=$b-$a; echo $c;
3. Regarding return calls.
a.php is as follows:<?php include("b.php"); echo "a"; ?>
<?php echo "b"; return; echo "b"; ?>
<?php include("b.php"); echo "a"; ?>
<?php echo "b"; exit; // 结束整个当前脚本 ?>
4. An important role of return: return value
Example 1:function test(){ $a=array(1,2); return $a; } $b=test(); print_r($b);
function test(){ $a=array(1,2); } $b=test(); print_r($b);
The above is the detailed content of How to use php return. For more information, please follow other related articles on the PHP Chinese website!