## 函数的多次返回怎么实现？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (24)

```function test(\$testvar)
{
// do something

return \$var1;
return \$var2;
}```

### 2 个回答

``````function wtf(\$blahblah = true) {
\$var1 = "ONe";
\$var2 = "tWo";

if(\$blahblah === true) {
return \$var2;
}
return \$var1;
}
``````

``````echo wtf();
//would echo: tWo
echo wtf("not true, this is false");
//would echo: ONe
``````

``````function wtf(\$blahblah = true) {
\$var1 = "ONe";
\$var2 = "tWo";

if(\$blahblah === true) {
return \$var2;
}

if(\$blahblah == "both") {
return array(\$var1, \$var2);
}

return \$var1;
}

echo wtf("both")[0]
//would echo: ONe
echo wtf("both")[1]
//would echo: tWo

list(\$first, \$second) = wtf("both")
// value of \$first would be \$var1, value of \$second would be \$var2
``````

``````function getXYZ()
{
return array(4,5,6);
}

list(\$x,\$y,\$z) = getXYZ();

// Afterwards: \$x == 4 && \$y == 5 && \$z == 6
// (This will hold for all samples unless otherwise noted)
``````

``````// note that I named the arguments \$a, \$b and \$c to show that
// they don't need to be named \$x, \$y and \$z
function getXYZ(&\$a, &\$b, &\$c)
{
\$a = 4;
\$b = 5;
\$c = 6;
}

getXYZ(\$x, \$y, \$z);
``````