Home >Backend Development >PHP Tutorial >The role of return keyword in php (including detailed explanation)
The role of return keyword in php
1. Terminate script execution
<?php echo '锄禾日当午<br>'; return;//终止脚本执行 echo '汗滴禾下土<br>';//不执行
*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 '锄禾日当午<br>'; require './test.php'; //包含文件 echo '汗滴禾下土<br>';
test.php
<?php echo 'aaa<br>'; return; //只能中断test.php echo 'bbb<br>';
Running result
锄禾日当午 aaa 汗滴禾下土
If you want to terminate completely Script execution, use exit(), or die()
echo 'aaa<br>'; exit(); //die() echo 'bbb<br>';
2, return page results
Example:
test.php
<?php return array('name'=>'tom','sex'=>'男');
demo.php
<?php $stu=require './test.php'; 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 'aaa'; return ; //终止函数执行 echo 'bbb'; } 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!