Home  >  Article  >  php教程  >  php函数的返回值

php函数的返回值

WBOY
WBOYOriginal
2016-05-25 16:47:261182browse

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