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

烟雨青岚
Release: 2023-04-08 19:28:01
forward
3390 people have browsed it

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;;//不执行
Copy after login

*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;;
Copy after login

test.php

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

Running result

锄禾日当午
aaa
汗滴禾下土
Copy after login

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

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

2, return page results

Example:

test.php

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

demo.php

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

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
Copy after login

Return value

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

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!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template