fb是什么意思| 宫颈轻糜是什么意思| 晨勃是什么意思| 夏天喝什么好| 吃什么立马排便| 慢性炎症是什么| 软组织肿胀是什么意思| 阿碧的居所叫什么名字| 缺碘有什么症状| 位图是什么意思| 狼吃什么| hepes缓冲液是什么| 九月份有什么节日| 什么是佝偻病有什么症状| 磷脂是什么| 高血压有什么症状表现| 草酸是干什么用的| 菊花和金银花一起泡水有什么效果| 体内湿气重吃什么药效果好| 煮馄饨放什么调料| 什么车性价比最高| 什么药能降肌酐| 什么分泌胰岛素| sr是什么意思| 小叶增生和乳腺增生有什么区别| 杜甫是什么派诗人| 2009属什么生肖| 木变石是什么| 手串18颗代表什么意思| 1935年是什么生肖| cems是什么意思| 开飞机需要什么驾照| 农历六月是什么生肖| 泌尿系统感染吃什么消炎药| hpv81阳性是什么意思| 儿童流鼻涕吃什么药| 7月1号是什么节日| 中位生存期什么意思| 沉香茶属于什么茶| 1980年属什么生肖| 没有精液是什么原因| 属猴和什么属相相克| 能戒烟的男人什么性格| 三角梅什么时候开花| 8月7日是什么星座| 肺结核是什么引起的| 什么食物含锌多| 鼠五行属什么| 11月27号什么星座| 今天吃什么菜| 男人有腰窝意味着什么| 薪字五行属什么| 肾结石发作有什么症状| pm是什么| 什么时候喝咖啡能减肥| 月煞是什么意思| 盐是什么| 牙根出血是什么原因| 花儿像什么比喻句| 沐什么意思| 为什么肚子总是胀胀的| 息肉和痔疮有什么区别| 桂枝茯苓丸治什么病| 刚怀孕吃什么最好最营养| 吃猪脑有什么好处和坏处| 医院康复科是干什么的| 沙发是什么意思| 11月16号是什么星座| 抗战纪念日为什么是9月3日| 团五行属什么| co是什么| 小分子肽能治什么病| pt是什么材质| 额头出汗多是什么原因| 吃什么补维生素b6| 奶绿是什么| 手抖挂什么科室| 1.30是什么星座| 纳音什么意思| 口臭是什么原因引起| 释怀什么意思| 蝉是什么生肖| 做生意的人最忌讳什么| 澳门买什么最便宜| 犬和狗有什么区别| 胆囊壁固醇沉积是什么意思| 手背肿是什么原因| 后脑勺出汗多是什么原因| 双侧胸膜局限性增厚是什么意思| 活血是什么意思| 癫狂是什么意思| 发达国家的标准是什么| 脚浮肿是什么原因| 上眼药什么意思| 过敏性皮炎用什么药| 拉架棉是什么面料| 什么工作赚钱最快| f代表什么| c反应蛋白是什么意思| 二级警监是什么级别| 罗红霉素治什么病| 19岁属什么的生肖| 病毒性肠胃炎吃什么药| 眉什么目什么| 小孩尿味道很重是什么原因| 钛对人体有什么好处| 尿频尿急尿不尽吃什么药最快见效| 什么饮料能解酒| 电影bd是什么意思| 狂犬疫苗挂什么科| perrier是什么水| 四月27日是什么星座| 手心脱皮是什么原因| 2010年属虎的是什么命| 活色生香的意思是什么| 十一月是什么星座| 为什么会突然流鼻血| 颈椎退行性病变是什么意思| 孕妇什么时候有奶水| 手信是什么| 腿上紫色血丝什么原因| 气血虚什么症状| 守望先锋是什么类型的游戏| 白细胞弱阳性是什么意思| 阳气不足是什么意思| 宝宝大便有泡沫是什么原因| 入睡困难是什么原因引起的| 心主什么| 甘草片不能和什么药一起吃| 夜黑风高什么意思| 梦见捡钱是什么意思| 老人爱睡觉什么征兆| rh是什么血型| 大战三百回合是什么意思| 6月7日什么星座| 乳果糖什么时候吃效果更佳| 女人耳垂大厚代表什么| 吃什么营养神经| 羟基丁酸在淘宝叫什么| 彼岸花什么时候开花| 电商属于什么行业| 伤官女是什么意思| 祭日和忌日是什么意思| 小孩咳嗽有痰吃什么药| 多囊不能吃什么食物| 蚂蝗怕什么| 宝宝缺钙吃什么补得快| 发生了什么| 出国用什么翻译软件好| 小指麻木是什么原因| 纤维增殖灶是什么意思| 梦见死了人是什么征兆| 蝾螈是什么| 肠易激综合征是什么原因造成的| 紫外线过敏吃什么药| 什么原因导致打嗝| 爵迹小说为什么不写了| 君子兰的寓意是什么| 难以启齿什么意思| 凌晨6点是什么时辰| 什么是代孕| 七月九号是什么日子| 一个鱼一个完读什么| 什么叫空调病| 脱肛吃什么药| 见分晓是什么意思| 小便解不出来是什么原因| 中国的国果是什么| 有什么别有病| 人言轻微是什么意思| 生地和熟地有什么区别| est.是什么意思| 沙拉酱是用什么做的| 紫笋茶属于什么茶| 吃什么可以补钙| 生理期没来是什么原因| lmp医学上什么意思| 复合维生素b什么时候吃最好| 择偶标准是什么意思| 心脏上有个小洞是什么病| 无话不谈是什么意思| 认干妈有什么讲究| 登革热是什么症状| 二拇指比大拇指长代表什么| 活色生香什么意思| 吃了火龙果小便红色是什么原因| 哆啦a梦为什么没有耳朵| 受害者是什么意思| 承欢膝下是什么意思| 韦编三绝什么意思| 为什么鼻毛会变白| 一人吃饱全家不饿是什么生肖| 梦见牙掉了一颗是什么意思| 摇花手是什么意思| 维生素d3什么牌子好| 紫颠是什么病怎样治| 阴道炎不能吃什么| 老炮儿是什么意思啊| 肾检查挂什么科| 水准仪是测量什么的| 眉骨疼是什么原因| gson是什么牌子| 得糖尿病的原因是什么| 胆红素偏高挂什么科| 2005属什么生肖| 眼睛飞蚊症用什么药能治好| 祭坛是什么意思| fev1是什么意思| 87年属什么的| 药引子是什么意思| 鬼佬是什么意思| 为什么来月经| 绝经什么意思| 飞字五行属什么| 上焦不通吃什么中成药| 正襟危坐什么意思| 为什么刚小便完又有尿意| 白茶适合什么季节喝| 王维有什么之称| 什么茶叶能减肥刮油脂肪| 八一建军节什么生肖| 低血压吃什么药| 酉是什么生肖| 真言是什么意思| 口气重吃什么药效果好| 面瘫有什么症状| 腋窝下疼痛是什么原因| 创面是什么意思| 蜂王浆是什么味道| 牙疼是什么原因引起的| 蜂胶是什么东西| 吃什么能降血脂| 体重除以身高的平方是什么指数| 热毒吃什么药| 恒心是什么意思| 心脏病吃什么食物好| 脂肪瘤看什么科| 睡着了放屁是什么原因| 同房出血是什么原因造成的| 浇去掉三点水读什么| 阑尾是什么器官| 汗水多吃什么药| 多巴胺是什么| 内能与什么因素有关| 深情款款什么意思| 梦见已故朋友什么预兆| 养神经吃什么食物最好| 发改局是做什么的| 七四年属什么生肖| 2.17是什么星座| 抗组胺是什么意思| 拉绿色的屎是什么原因| 早上咳嗽是什么原因| 维生素e有什么作用| 寻麻疹涂抹什么药膏| 白雪什么| 中国国菜是什么菜| 无心是什么意思| 感冒反复发烧是什么原因引起的| 孕妇喝什么茶对胎儿好| 股票解禁是什么意思| 岳飞属什么生肖| 内痔有什么症状与感觉| 肋骨骨折什么症状| 百度
function returns multiple times
P粉022285768
P粉022285768 2025-08-04 15:58:34
0
2
779

Is it possible to have a function with two return values ??like this:

function test($testvar)
{
  // Do something

  return $var1;
  return $var2;
}

If so, how can I get each return separately?

P粉022285768
P粉022285768

reply all(2)
P粉548512637

Technically, you cannot return multiple values. However, there are ways to work around this limitation. The most similar way to return multiple values ??is to use the list keyword:

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)

Technically, you return an array and use list to store the elements of that array in different values, rather than storing the actual array. Using this technique will make it feelmost like returning multiple values.

list The solution is a php specific one. Some languages ??have similar structures, but many more don't. There is another method that is commonly used to "return" multiple values, and is available in almost every language (in one way or another). However, this approach looks completely different, so it may take some getting used to.

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

This technology is also used in some functions defined by PHP itself (for example, # in str_replace, $matches in preg_match). This might feel a lot different than returning multiple values, but it's at least worth knowing about.

The third method is to use an object to hold the different values ??you need. This requires more input, so it's used less frequently than the two methods above. Still, it might make sense to use this feature when the same set of variables is used in multiple places (or, of course, in a language that doesn't support the above method or allows you to do it without additional typing).

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;
The above method summarizes the main ways for functions to return multiple values. However, there are some variations on these methods. The most interesting variants are those that actually return an array, since you can do a lot with arrays in PHP.

First, we can simply return an array without treating it as anything other than an array:

function getXYZ()
{
    return array(1,2,3);
}

$array = getXYZ();

$x = $array[0];
$y = $array[1];
$z = $array[2];
The most interesting part of the code above is that the code inside the function is the same as in the first example I provided; only the code that calls the function has changed. This means that what the caller of the function does with the results returned by the function is up to the person who called the function.

Alternatively, you can use an associative array:

function getXYZ()
{
    return array('x' => 4,
                 'y' => 5,
                 'z' => 6);
}

$array = getXYZ();

$x = $array['x'];
$y = $array['y'];
$z = $array['z'];
Php does have the

compact function which allows you to do the same thing as above but write less code. (Well, the example won't have less code, but the real-world application probably will.) However, I think the typing savings are minimal, and it makes the code harder to read, so I wouldn't do it myself. However, here is an example:

function getXYZ()
{
    $x = 4;
    $y = 5;
    $z = 6;
    
    return compact('x', 'y', 'z');
}

$array = getXYZ();

$x = $array['x'];
$y = $array['y'];
$z = $array['z'];
It should be noted that while

compact does have a counterpart in extract that can be used in the calling code here, since using it is a bad idea ( Especially for something as simple as this), I won't even give a sample of it. The problem is, it "magically" creates the variables for you, and you can't see what variables are created without looking in other parts of the code.

Finally, I would like to mention that list does not work well with associative arrays. The following will meet your expectations:

function getXYZ()
{
    return array('x' => 4,
                 'y' => 5,
                 'z' => 6);
}

$array = getXYZ();

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

However, the following will do something different:

function getXYZ()
{
    return array('x' => 4,
                 'z' => 6,
                 'y' => 5);
}

$array = getXYZ();

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

// Pay attention: $y == 6 && $z == 5

If you use list with an associative array, and someone else has to change the code in the called function in the future (which can happen in any case), it may break suddenly, so I recommend Do not use list arrays with associative arrays.

P粉717595985

Cannot return 2 variables. However, you can propagate an array and return it; create conditions to return dynamic variables, etc.

For example, this function will return $var2

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

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

In app:

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

If you want both, you can slightly modify the function

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
爿是什么意思 亚玛病是什么病 脑出血是什么原因造成的 什么血型不招蚊子 红色药片一般是什么药
洗纹身去医院挂什么科 什么的假山 吴佳尼为什么嫁马景涛 屋后有坟有什么影响吗 碧玺是什么
雷人是什么意思啊 和胃是什么意思 孕期长痘痘是什么原因 什么虎不吃人 羊水指数和羊水深度有什么区别
早泄是什么原因导致 蟑螂屎长什么样 苹可以组什么词 静修是什么意思 两个脚脖子肿什么原因
脾胃阴虚吃什么中成药hcv9jop3ns6r.cn 不自觉摇头是什么病hcv8jop0ns1r.cn 白粥配什么菜好吃hcv8jop1ns7r.cn 什么是癌胚抗原hcv8jop5ns8r.cn 红花是什么生肖hcv7jop9ns9r.cn
审计署是什么级别hcv9jop3ns7r.cn 膝盖痛是什么原因hcv9jop2ns6r.cn 高压低是什么原因hcv9jop6ns7r.cn boys是什么意思xjhesheng.com 导盲犬一般是什么品种hcv7jop4ns5r.cn
wedding什么意思hcv9jop1ns5r.cn 早上空腹喝淡盐水有什么好处hcv8jop6ns7r.cn 世界上最长的单词是什么hcv8jop7ns0r.cn 石膏的主要成分是什么hcv8jop1ns2r.cn 广肚是什么hcv8jop9ns3r.cn
潜伏是什么意思hcv7jop5ns4r.cn 为什么会早泄hcv9jop0ns3r.cn 燕窝什么季节吃最好hcv9jop4ns4r.cn 黄体酮吃了有什么副作用hcv8jop2ns4r.cn 身上长疣是什么原因hcv9jop3ns1r.cn
百度