3 回答
TA贡献1852条经验 获得超1个赞
在您的示例中,第二次返回永远不会发生-第一次返回是PHP将运行的最后一次返回。如果需要返回多个值,则返回一个数组:
function test($testvar) {
return array($var1, $var2);
}
$result = test($testvar);
echo $result[0]; // $var1
echo $result[1]; // $var2
TA贡献1877条经验 获得超1个赞
$var2
function wtf($blahblah = true) {
$var1 = "ONe";
$var2 = "tWo";
if($blahblah === true) {
return $var2;
}
return $var1;}echo wtf();//would echo: tWoecho wtf("not true, this is false");//would echo: ONefunction 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: ONeecho wtf("both")[1]
//would echo: tWolist($first, $second) = wtf("both")// value of $first would be $var1, value of $second would be $var2TA贡献1805条经验 获得超10个赞
list
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)list
list
// note that I named the arguments $a, $b and $c to show that// they don't need to be named $x, $y and $zfunction getXYZ(&$a, &$b, &$c){
$a = 4;
$b = 5;
$c = 6; }getXYZ($x, $y, $z);$count$matches
class MyXYZ{
public $x;
public $y;
public $z;}function getXYZ(){
$out = new MyXYZ();
$out->x = 4;
$out->y = 5;
$out->z = 6;
return $out;}$xyz = getXYZ();$x = $xyz->x;$y = $xyz->y;$z = $xyz->z;function getXYZ(){
return array(1,2,3);}$array = getXYZ();$x = $array[1];$y = $array[2];$z = $array[3];function getXYZ(){
return array('x' => 4,
'y' => 5,
'z' => 6);}$array = getXYZ();$x = $array['x'];$y = $array['y'];$z = $array['z'];compact
function getXYZ(){
$x = 4;
$y = 5;
$z = 6;
return compact('x', 'y', 'z');}$array = getXYZ();$x = $array['x'];$y = $array['y'];$z = $array['z'];compactextract
list
function getXYZ(){
return array('x' => 4,
'y' => 5,
'z' => 6);}$array = getXYZ();list($x, $y, $z) = getXYZ();function getXYZ(){
return array('x' => 4,
'z' => 6,
'y' => 5);}$array = getXYZ();list($x, $y, $z) = getXYZ();// Pay attention: $y == 6 && $z == 5listlist
- 3 回答
- 0 关注
- 616 浏览
添加回答
举报
