从函数返回两个值 [英] Returning two values from a function

查看:75
本文介绍了从函数返回两个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用将输出值的函数时是否可以返回两个值?

Is it possible to return two values when calling a function that would output the values?

例如,我有这个:

<?php
    function ids($uid = 0, $sid = '')
    {
        $uid = 1;
        $sid = md5(time());

        return $uid;
        return $sid;
    }

    echo ids();
?>

哪个将输出1.我想选择要输出的内容,例如ids($sid),但仍会输出1.

Which will output 1. I want to chose what to ouput, e.g. ids($sid), but it will still output 1.

有可能吗?

推荐答案

您只能返回一个值.但是您可以使用本身包含其他两个值的数组:

You can only return one value. But you can use an array that itself contains the other two values:

return array($uid, $sid);

然后您访问以下值:

$ids = ids();
echo $ids[0];  // uid
echo $ids[1];  // sid

您还可以使用关联数组:

You could also use an associative array:

return array('uid' => $uid, 'sid' => $sid);

并访问它:

$ids = ids();
echo $ids['uid'];
echo $ids['sid'];

这篇关于从函数返回两个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆