php 从函数返回两个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3815234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:12:18  来源:igfitidea点击:

Returning two values from a function

phpfunctionreturn-value

提问by MacMac

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

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

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

Is it even possible?

甚至有可能吗?

回答by Gumbo

You can only return one value. But you can use an arraythat 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'];

回答by Annika Backstrom

Return an array or an object if you need to return multiple values. For example:

如果需要返回多个值,则返回一个数组或一个对象。例如:

function foo() {
    return array(3, 'joe');
}

$data = foo();
$id = $data[0];
$username = $data[1];

// or:
list($id, $username) = foo();

回答by Jerome WAGNER

You can use an array and the list function to get the info easily :

您可以使用数组和列表函数轻松获取信息:

function multi($a,$b) {
   return array($a,$b);
}

list($first,$second) = multi(1,2);

I hope this will help you

我希望这能帮到您

Jerome

杰罗姆

回答by Mark Baker

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

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

$t = ids(); 
echo $t['uid'],'<br />';
echo $t['sid'],'<br />';

回答by NikiC

Many possibilities:

多种可能性:

// return array
function f() {
    return array($uid, $sid);
}
list($uid, $sid) = f();
$uid;
$sid;

// return object
function f() {
    $obj = new stdClass;
    $obj->uid = $uid;
    $obj->sid = $sid;
    return $obj;
}
$obj->uid;
$obj->sid;

// assign by reference
function f(&$uid, &$sid) {
    $uid = '...';
    $sid = '...';
}
f($uid, $sid);
$uid;
$sid;

回答by user2029015

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

and then access it like

然后像这样访问它

$dinga = ids();
extract($dinga);

extractwill make uid and sid variable. So then you can use $uid and $sid.

提取将使 uid 和 sid 变量。那么你可以使用 $uid 和 $sid。

回答by Adrian Cumpanasu

Pass by reference would be a solution. This way you:

通过引用传递将是一个解决方案。这样你:

  • return a value as you normally would

  • and modify the second (the one passed by refference to the function)

  • 像往常一样返回一个值

  • 并修改第二个(通过引用函数传递的那个)

An example:

一个例子:

function foo(&$var) // notice the & in front of the parameter passed
{
    $var++;
    return 1;
}

$a=5;
echo foo($a);
// echoes 1, the returned value
echo $a;
// $a is now 6