99 Bottles of Beer in PHP

I've been reading Chad Fowler's "The Passionate Programmer: Creating a Remarkable Career in Software Development (Pragmatic Life)", and in one chapter he introduced the Code Kata, which is a language agnostic series of challenges to stretch your imagination and creativity in programming.

Chad also mentions writing a "99 Bottles of Beer" program.

I chose PHP to do the exercise and came up with probably the simplest example of it.

I am really curious as to how others might create the same result and invite anyone to post their own version of this.

There are no rules. It can be as complicated or as simple. In fact that is the point of the exercise! Can you come up with the simplest iteration and then the most complicated iteration?

How about wrapping it in a function?

<?php
$bottles
= 99;
  while (
$bottles > 0) {
    echo
"$bottles bottles of beer on the wall, ";
    echo
"$bottles bottles of beer,";
    echo
"take one down and pass it around...</br>";
   
$bottles--;
}
if(
$bottles > 1) {
echo
"</p>Time to buy more beer!";
}
?>

Comments

As someone that really sucks at PHP by putting it in this context 'beer' I am beginning to understand the functions a little better :) http://davidhouk.com

I'm glad it helped. This is a very simple example and could be done in a lot of different ways.

Add new comment