Home  >  Article  >  Backend Development  >  The role of return keyword in php (including detailed explanation)

The role of return keyword in php (including detailed explanation)

烟雨青岚
烟雨青岚forward
2020-06-08 13:36:153389browse

The role of return keyword in php (including detailed explanation)

The role of return keyword in php

1. Terminate script execution

<?php
echo &#39;锄禾日当午<br>&#39;;
return;//终止脚本执行
echo &#39;汗滴禾下土<br>&#39;;//不执行

*Reminder: return can only interrupt the current page. If there is an included file, it can only interrupt the included file.

Example:

demo.php

<?php
echo &#39;锄禾日当午<br>&#39;;
require &#39;./test.php&#39;;    //包含文件
echo &#39;汗滴禾下土<br>&#39;;

test.php

<?php
echo &#39;aaa<br>&#39;;
return;   //只能中断test.php
echo &#39;bbb<br>&#39;;

Running result

锄禾日当午
aaa
汗滴禾下土

If you want to terminate completely Script execution, use exit(), or die()

echo &#39;aaa<br>&#39;;
exit();  //die()
echo &#39;bbb<br>&#39;;

2, return page results

Example:

test.php

<?php
return array(&#39;name&#39;=>&#39;tom&#39;,&#39;sex&#39;=>&#39;男&#39;);

demo.php

<?php
$stu=require &#39;./test.php&#39;;
print_r($stu);  //Array ( [name] => tom [sex] => 男 )

Summary: Use this method to introduce configuration files into the project.

3. Return and termination of function

return is used in functions:

⑴ Terminate function execution

function fun() {
echo &#39;aaa&#39;;
return ;  //终止函数执行
echo &#39;bbb&#39;;
}
fun();   //aaa

Return value

function fun() {
return 10;  //返回值
}
echo fun();  //10

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of The role of return keyword in php (including detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete