Search code examples
phpwhile-looptrailing

How to remove trailing comma after final number in while loop


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. ",";
}

Solution

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

    echo implode(',', range(1, 10));