1 to 1000 in PHP With No Conditionals or Loops

Inspired by this post (a puzzle challenge asking if you could think of a way to print from 1-1000 in C++ with no conditionals or loops)

I wondered if I could think of a hack to do it in PHP… here’s my hack!

<?php
   sleep(1);
   $i = ++$_GET['i'];
   $go[1000] = 'http://google.com?';
   header("Location: {$go[$i]}/?i=$i");
?>

It’s a PHP script (index.php) running from the root of a server with warnings switched off.

As an added bonus you need to watch http live headers to see the “print” out 😉

(Note: It’ won’t work in most modern browsers as they tend to have a limit of 20 redirects. Meh. That’s just a technicality.)

 



Check out my tech startup podcast TechZing. Maximise your downtime by listening to us while you code, commute, work-out or do the dishes!

Comments

  • Hehe. I like how you used HTTP for it.

    I noticed that same thread as well, earlier today. So when your tweet about this post popped up just now I couldn’t resist giving it a try myself!

    http://vvv.tobiassjosten.net/php/php-to-1000-without-conditionals-and-loops

  • Comments closed