从函数中返回2个值 [英] Returning 2 values from a function

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

问题描述

是否有可能在调用可能输出值的函数时返回两个值,例如,我有这个:

Is it possible to return two values when calling a function that would output the values, for example, I have this:

<?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);

然后您可以访问以下值:

Then you access the values like:

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

您也可以使用关联数组:

You could also use an associative array:

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

访问它:

And accessing it:

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

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

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