# Display 0 - 1000 - 0 each on a separate line

I've tried this question on two forums and have been told this is the best place for this type of question. I'm trying to print from 0 to 1000 then back to 0 again (and all numbers in-between), printing 1000 only once. Also, each number must be on a separate line using vanilla PHP.

My current code is 60 characters long. Is there a way to do the same thing but in less characters?

### PHP, 60 bytes

echo implode("<p>",array_merge(range(0,999),range(1000,0)));

Try it online!

• Welcome! This looks like a solid tips question, but I suggest taking a look at this list and see if there's any more helpful info you can give. Where did the challenge originally come from?
– xnor
Mar 17 '21 at 11:29
• hi, thanks!. i'm studying on a course and its just like a bit of fun set by the trainer. i cant link the challenge because its on a private git hub.
– dev
Mar 17 '21 at 11:31
• I think it would be good to link a runnable version of your code. Is this right? I don't know PHP so I'm not sure if I did it right with the <?php around the code and the <p> for newlines. You can edit the code and the press the link icon at the top, and the copy-paste option for "Code Golf submission" can be pasted into your question.
– xnor
Mar 17 '21 at 11:36
• Also, we have Tips for golfing in PHP which might give you some inspiration. I noticed one mentioning array_merge which your code uses.
– xnor
Mar 17 '21 at 11:37
• May the list end with a trailing <p>? Mar 17 '21 at 11:40

# PHP, 38 bytes

for(;$i-=1|$x|=$i/1001;)echo~$i,"<p>";

Try it online!

Try it online!

# 43 bytes

If the output can end with an extra <p>:

for(;$n<2001;)echo 1e3-abs(1e3-$n++),'<p>';

Try it online!

• i was trying turnery but couldn't get it to work.
– dev
Mar 17 '21 at 11:43
• echo takes multiple arguments, so you can remove the brackets and replace . with , Mar 17 '21 at 11:47
• It looks like 1e3-abs($n-1e3) is a byte shorter than the ternary. And shifting indices probably shortens that too. – xnor Mar 17 '21 at 11:49 • for(;$i<2001;)echo 1e3-abs(1e3-$i++),'<p>'; (thx @xnor) Mar 17 '21 at 11:49 • I think echo 0;for(;$n<2e3;)echo'<p>',min(++$n,2e3-$n); is shorter for the first option, but I couldn't seem to adapt it for the second option.
– Neil
Mar 17 '21 at 13:24

# PHP, 51 bytes

echo join("<p>",[...range(0,999),...range(1e3,0)]);

Try it online!

There's already shorter versions, but I just wanted to show the OP how the original code can be shortened with the old alias join and the spread operator that is now available in PHP

Depending on your rules, even shorter:

49 bytes with <?=

<?=join("<p>",[...range(0,999),...range(1e3,0)]);

or even 47 with a literal new line, if your output can be wrapped in <pre><code>

<?=join("
",[...range(0,999),...range(1e3,0)]);