Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Write a countdown script in PHP that starts at 10 and ends at 1.

Here is the entire question:

Write a countdown script in PHP that starts at 10 and ends at 1. For every odd number I want you to add 1 to the number and every even number I want you to subtract 1 from the number. So the final countdown will look like:

9 10 7 8 5 6 3 4 1 2

1 Answer

I would use for loop & ternary operators to get it as small and clean as possible. Something like this:

for($i = 10; $i >= 1; $i--) {
    $b = ($i % 2) == 0 ? $i -1 : $i + 1;
    echo $b . "\n";
}