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

php函数的返回值

WBOY
Release: 2016-05-25 16:47:26
Original
1181 people have browsed it

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

执行结果如:

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

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

输出结果:

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

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

实例代码如下:

<?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; //这是函数返回值;
?>
Copy after login


本文地址:

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

Related labels:
source:php.cn
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!