user3740970 user3740970 - 1 year ago 100
PHP Question

How to insert a hyphen between numbers in PHP?

I want to make a script in PHP which displays the numbers and a hyphen between the numbers. It should look like this:


I have the following script right now, but the problem is it displays a hyphen at the end of the numbers:

$x = 1;
$h = 1;
while($x <= 10) {
echo "$x";

if($h < $x){
echo "-";

Thanks in advance :)

Answer Source

There is simpler way to do this work. Use range() to create array contain numbers and use implode() to join target array with string.

echo implode("-", range(1, 10));

See result in demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download