有没有办法让这段代码缩短$ val1 $ val2 .......的值是查询的结果
$val1 = 1;
$val2 = 2;
$val3 = 3;
$val4 = 4;
$val5 = 5;
$val6 = 8;
$val7 = 7;
$val8 = 8;
$val9 = 8;
$val10 = 9;
$val11 = 13;
$val12 = 12;
echo $val1.",".$val2.",".$val3.",".$val4.",".$val5.",".$val6.",".$val7.",".$val8.",".$val9.",".$val10.",".$val11.",".$val12;
?>
有没有办法简化这个
echo $val1.",".$val2.",".$val3.",".$val4.",".$val5.",".$val6.",".$val7.",".$val8.",".$val9.",".$val10.",".$val11.",".$val12;
答案 0 :(得分:3)
$values = array(1, 2, 3, 4, 5, 8, 7, 8, 8, 9, 13, 12);
echo implode(',', $values);
答案 1 :(得分:1)
这就是数组的用途:
$vals = range(1, 12);
echo implode(',', $vals);
如果您没有第一手创建数组,则应首先将编号的变量转换为数组:
$vals = array($var1, $var2, $var3, ... , $var12);
echo implode(',', $vals);
对于单个echo调用,这不会有太大变化,但通常可以在代码中将其移动得更多,以降低复杂性。
答案 2 :(得分:0)
把它放在一个数组中并循环它可能会有效。
$values = array(1, 2, 3, 4, 5, 6);
foreach ($values as $key => $value) {
echo $value;
}