0

Trying to remove the comma that comes after the number 10. Tried every plausible workaround, but nothing's worked so far.

$i = 0;

while($i < 10) {
echo ++$i. ",";
}

2 Answers2

3

Don't use a loop in the first place. Use implode() to insert a delimiter between array elements.

echo implode(',', range(1, 10));
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You can do it using loop and rtrim() like this:

$i = 0;
$result = '';
while($i < 10) {
  $result .= ++$i. ",";
}

echo rtrim($result, ',');
Ankur Mishra
  • 1,284
  • 1
  • 6
  • 15