使用PHP的`list`函数被认为是不好的做法?

时间:2014-07-23 20:30:29

标签: php

由于列表是一个非常常见的词,因此无法搜索此主题。

list($total, $tax) = $this->getOrderAmounts();

这被认为是不好的做法还是代码味?我觉得我最近一直把它当作一条不好的捷径。

除此之外:list实际上是一种语言结构,而不是一种功能

1 个答案:

答案 0 :(得分:4)

我个人不会使用列表,因为它是如此"不准确"。考虑一下: -

$rainbow = array('violet', 'indigo','blue','green','yellow','orange','red');
list($violet,,,,,,$red) = $rainbow;

echo "$violet has low wavelength and $red has highest wavelength"; //Outputs violet has low wavelength and red has highest wavelength

但如果你犯了错误: -

$rainbow = array('violet', 'indigo','blue','green','yellow','orange','red');
list($violet,,,,,$red) = $rainbow;

echo "$violet has low wavelength and $red has highest wavelength"; //violet has low wavelength and orange has highest wavelength

这会更容易: -

$rainbow = array('violet', 'indigo','blue','green','yellow','orange','red');

echo "$rainbow[0] has low wavelength and $rainbow[6] has highest wavelength";