Home > php教程 > php手册 > body text

实例详解php中的return语句

WBOY
Release: 2018-10-26 17:20:48
forward
1051 people have browsed it

这篇文章主要介绍了php中的return语句 ,有一定的参考价值,感兴趣的朋友可以看看, 希望对你哟所帮助!

一个函数中可以有return语句,也可以没有。什么时候需要呢?就是当你想接收函数的执行结果的时候,你需要一个return 语句。

函数执行完return语句之后,return语句后面的代码是永远不会得到执行的。

PHP中的return一般情况下只能通过return获得一个返回值,如果想获得多个返回值,可以返回一个数组。

1 <?php2 3 function sayHello($a,$b)4 {5     return $a;6     return $b;7 }8 9 ?>
Copy after login

想通过两个return语句获得两个返回值,但结果是只能返回a的值。

下图展示了一个用return返回数组的例子。

sayHello函数通过return返回一个数组,用变量temp来接收这个数组,再通过foreach循环输出返回的值。

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

Related labels:
source:cnblogs.com
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!