<?php
$numbers = array("12", "-32", "52", "-65", "98");
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
如何添加+符号不是 - 减去。
结果: +12, -32, +52, -65, 98
答案 0 :(得分:5)
你可以添加这样的条件:
echo (int)$numbers[$x] > 0 ? '+'.$numbers[$x] : $numbers[$x];
所以它将是
$numbers = array("12", "-32", "52", "-65", "98");
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo ((int)$numbers[$x] > 0) ? '+'.$numbers[$x] : $numbers[$x];
echo "<br>";
}
答案 1 :(得分:1)
对于非常直接的前向回声修改,请替换
echo $numbers[$x];
与
echo ( $numbers[$x] > 0 ) ? '+'.$numbers[$x] : $numbers[$x];