从函数返回2个值[duplicate]

时间:2020-11-29 09:50:57

This question already has an answer here:

这个问题已经有了答案:

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,我想选择要输出的内容,例如id ($sid),但是它仍然输出1。

Is it even possible?

它甚至有可能吗?

7 个解决方案

#1


56  

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


17  

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

#3


12  

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

杰罗姆

#4


6  

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 />';

#5


4  

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;

#6


2  

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)

    修改第二个(通过refference传递给函数的那个)

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

#7


2  

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

and then access it like

然后像这样访问它

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

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

提取将使uid和sid变量。然后你可以使用$uid和$sid。

#1


56  

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


17  

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

#3


12  

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

杰罗姆

#4


6  

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 />';

#5


4  

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;

#6


2  

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)

    修改第二个(通过refference传递给函数的那个)

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

#7


2  

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

and then access it like

然后像这样访问它

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

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

提取将使uid和sid变量。然后你可以使用$uid和$sid。