爿是什么意思 | 亚玛病是什么病 | 脑出血是什么原因造成的 | 什么血型不招蚊子 | 红色药片一般是什么药 |
洗纹身去医院挂什么科 | 什么的假山 | 吴佳尼为什么嫁马景涛 | 屋后有坟有什么影响吗 | 碧玺是什么 |
雷人是什么意思啊 | 和胃是什么意思 | 孕期长痘痘是什么原因 | 什么虎不吃人 | 羊水指数和羊水深度有什么区别 |
早泄是什么原因导致 | 蟑螂屎长什么样 | 苹可以组什么词 | 静修是什么意思 | 两个脚脖子肿什么原因 |
脾胃阴虚吃什么中成药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 |
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: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.This technology is also used in some functions defined by PHP itself (for example, # in str_replace, $matches in preg_match
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).). This might feel a lot different than returning multiple values, but it's at least worth knowing about.
compact
It should be noted that whilefunction 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:
compact
does have a counterpart in
extractthat 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:However, the following will do something different:
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 uselist
arrays with associative arrays.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
In app:
If you want both, you can slightly modify the function