search
Homephp教程php手册php函数的返回值

php函数的返回值.其实php函数可以返回一个或多个值,使用return关键字可以返回一个变量或者一个数组.return会使程序在return处停止,并返回指定的变量.今天举一个例子吧:

实例代码如下:

<?php
function she($a, $b, $c) {
    return array(
        $c,
        $b,
        $a
    );
}
list($x, $y, $z) = she(2, 3, 4);
echo &#39;$x=&#39; . $x . &#39;$y=&#39; . $y . &#39;$z=&#39; . $z;
?>

执行结果如: 

<?php
function add($shu) {
    return $shu + 1;
}
echo add(2) . &#39; 
&#39;;
function she($a, $b, $c) {
    return array(
        $c,
        $b,
        $a
    );
}
list($x, $y, $z) = she(2, 3, 4);
echo &#39;$x=&#39; . $x . &#39; 
$y=&#39; . $y . &#39; 
$z=&#39; . $z;
?>

php函数,想要传回多个返回值,怎么做到(函数不能返回多个值,但可以通过返回一个数组来得到类似的效果.)

实例代码如下:

<?php
function results($string) {
    $result = array();
    $result[] = $string; //原字符串
    $result[] = strtoupper($string); //全部换成大写
    $result[] = strtolower($string); //全部换成小写
    $result[] = ucwords($string); //单词的首字母换成大写
    return $result;
}
$multi_result = results(&#39;The quick brown fox jump over the lazy dog&#39;);
print_r($multi_result);
?>

输出结果:  

Array  
(  
[0] => The quick brown fox jump over the lazy dog  
[1] => THE QUICK BROWN FOX JUMP OVER THE LAZY DOG  
[2] => the quick brown fox jump over the lazy dog  
[3] => The Quick Brown Fox Jump Over The Lazy Dog  
)

引用,本函数返回三个值,一个是函数返回,两个传引用.

实例代码如下:

<?php
function test(&$a, &$b) {
    $a = 1000;
    $b = 12000;
    return $a + $b;
}
$a = 10;
$b = 12;
$c = test($a, $b); //注意这里没有 & 了.
//显示修改后的值
echo $a;
echo $b;
echo $c; //这是函数返回值;
?>


本文地址:

转载随意,但请附上文章地址:-)

Statement
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
C语言return的用法详解C语言return的用法详解Oct 07, 2023 am 10:58 AM

C语言return的用法有:1、对于返回值类型为void的函数,可以使用return语句来提前结束函数的执行;2、对于返回值类型不为void的函数,return语句的作用是将函数的执行结果返回给调用者;3、提前结束函数的执行,在函数内部,我们可以使用return语句来提前结束函数的执行,即使函数并没有返回值。

Java中return和finally语句的执行顺序是怎样的?Java中return和finally语句的执行顺序是怎样的?Apr 25, 2023 pm 07:55 PM

源码:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#输出上述代码的输出可以简单地得出结论:return在finally之前执行,我们来看下字节码层面上发生了什么事情。下面截取case1方法的部分字节码,并且对照源码,将每个指令的含义注释在

Vue3怎么使用setup语法糖拒绝写returnVue3怎么使用setup语法糖拒绝写returnMay 12, 2023 pm 06:34 PM

Vue3.2setup语法糖是在单文件组件(SFC)中使用组合式API的编译时语法糖解决Vue3.0中setup需要繁琐将声明的变量、函数以及import引入的内容通过return向外暴露,才能在使用的问题1.在使用中无需return声明的变量、函数以及import引入的内容,即可在使用语法糖//import引入的内容import{getToday}from&#39;./utils&#39;//变量constmsg=&#39;Hello!&#39;//函数func

详解JavaScript函数返回值和return语句详解JavaScript函数返回值和return语句Aug 04, 2022 am 09:46 AM

JavaScript 函数提供两个接口实现与外界的交互,其中参数作为入口,接收外界信息;返回值作为出口,把运算结果反馈给外界。下面本篇文章带大家了解一下JavaScript函数返回值,浅析下return语句的用法,希望对大家有所帮助!

Python返回值return怎么用Python返回值return怎么用Oct 07, 2023 am 11:10 AM

Python返回值return用法是当函数执行到return语句时,将立即停止执行,并将指定的值返回给调用函数的地方。详细用法:1、返回单个值;2、返回多个值;3、返回空值;4、提前结束函数的执行。

使用JavaScript中return关键字使用JavaScript中return关键字Feb 18, 2024 pm 12:45 PM

JavaScript中return的用法,需要具体代码示例在JavaScript中,return语句用于指定从函数中返回的值。它不仅可以用于结束函数的执行,还可以将一个值返回给调用函数的地方。return语句有以下几个常见的用法:返回一个值return语句可以用来返回一个值给调用函数的地方。下面是一个简单的示例:functionadd(a,b){

JavaScript中如何使用return语句JavaScript中如何使用return语句Feb 26, 2024 am 09:21 AM

JavaScript中return的使用方法,需要具体代码示例在JavaScript中,return是一个非常重要的关键字,它通常用于函数中返回值或结束函数的执行。return语句用于将值返回给函数的调用者,并终止函数的执行。return语句可以在函数的任何位置使用,并且可以返回任何JavaScript数据类型,包括数字、字符串、布尔值、

在Java中,一个方法中的return语句执行后,finally块会被执行吗?在Java中,一个方法中的return语句执行后,finally块会被执行吗?Sep 17, 2023 pm 03:05 PM

是的,即使在方法中的return语句之后,finally块也会被执行。Java中无论是否发生异常,finally块都会执行。如果我们在finally块中显式调用System.exit()方法,那么只有它不会被执行。很少有情况不会执行finally,例如JVM崩溃、电源故障、软件崩溃等。除了这些情况外,finally块将始终被执行。示例publicclassFinallyBlockAfterReturnTest{&nbsp;&nbsp;publicstaticvoidmain(St

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),