SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My version of 99 bottles of beer on the wall..needs some tweaking...

    Hi everybody, here's my take on creating a script that displays the lyrics for the song 99 Bottles of Beer.

    PHP Code:
    <?php

    $beer 
    99;

    while(
    $beer >= 0)  {
      echo(
    $beer.' bottles of beer on the wall, '.$beer.' bottles of beer,<br/>');
      echo(
    'Take one down and pass it around, '.($beer-1).' bottles of beer on the wall.<br/>');
      echo(
    '<br/>');

      
    $beer--;

    ?>
    How it turned out in the browser?

    99 bottles of beer on the wall, 99 bottles of beer,
    Take one down and pass it around, 98 bottles beer on the wall.

    98 bottles of beer on the wall, 98 bottles of beer,
    Take one down and pass it around, 97 bottles beer on the wall.

    ...................................
    .......................................

    2 bottles of beer on the wall, 2 bottles of beer,
    Take one down and pass it around, 1 bottles beer on the wall.

    1 bottles of beer on the wall, 1 bottles of beer,
    Take one down and pass it around, 0 bottles beer on the wall.

    0 bottles of beer on the wall, 0 bottles of beer,
    Take one down and pass it around, -1 bottles beer on the wall.

    Now there are some flaws, the actual lyrics at the end of the song goes like this...

    2 bottles of beer on the wall, 2 bottles of beer,
    Take one down and pass it around, 1 bottle of beer on the wall.

    1 bottle of beer on the wall, 1 bottle of beer,
    Take one down and pass it around, no more bottles of beer on the wall.

    No more bottles of beer on the wall, no more bottles of beer,
    Go to the store and buy some more, 99 bottles of beer on the wall.

    Any thoughts and views on how to perfect it would be greatly appreciated.

    Thanking you in advance,
    Jaysone

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about simply this?
    PHP Code:
    $beer 99;
    while(
    $beer >= 0)  {
      if(
    $beer-== 0){
          echo(
    $beer.' bottles of beer on the wall, '.$beer.' bottles of beer,<br/>');
          echo(
    'Take one down and pass it around, no more bottles of beer on the wall.<br/>');
      }
      else if(
    $beer-0){
          echo(
    'No more bottles of beer on the wall, no more bottles of beer,<br/>');
          echo 
    'Go to the store and buy some more, 99 bottles of beer on the wall.';
      }
      else{
          echo(
    $beer.' bottles of beer on the wall, '.$beer.' bottles of beer,<br/>');
          echo(
    'Take one down and pass it around, '.(($beer == )).' bottles of beer on the wall.<br/>');
      }
      echo(
    '<br/>');
      
    $beer--;

    Not much hard tricks :P
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    for ($i=99$i>2$i--) {
        
    printf('
    %d bottles of beer on the wall, %d bottles of beer,
    Take one down and pass it around, %d bottles beer on the wall.
    '
    $i$i$i 1);
    }

    echo 
    '
    2 bottles of beer on the wall, 2 bottles of beer,
    Take one down and pass it around, 1 bottle of beer on the wall.

    1 bottle of beer on the wall, 1 bottle of beer,
    Take one down and pass it around, no more bottles of beer on the wall.

    No more bottles of beer on the wall, no more bottles of beer,
    Go to the store and buy some more, 99 bottles of beer on the wall.
    '


  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah lol if you want it to be perfect just print out the lyrics:

    PHP Code:
    $lyrics '99 bottles....';
    echo 
    $lyrics
    I'm assuming you want the code to generate it.

    first of all I wouldn't use a while. while's can be nasty to try and avoid them if you don't need it.

    PHP Code:
    <?php

    for ($i=99$i>-1$i--)
        echo 
    lyricLine($i);

    function 
    lyricLine($i)
    {
        return 
    '<p>'.number($itrue).' bottle'.s($i).' of beer on the wall, '.number($ifalse).' bottle'.s($i).' of beer,<br />'.
            (
    $i == 'Go to the store and buy some more, 99' 'Take one down and pass it around, '.number($i-1false) ).' bottle'.s($i-1).' of beer on the wall.</p>';
    }

    function 
    s($i)
    {
        return 
    $i != 's' '';
    }

    function 
    number($i$caps)
    {
        
    $out $i $i 'No more';
        if (!
    $caps$out strtolower($out);
        return 
    $out;
    }

    ?>

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by antirealm View Post
    yeah lol if you want it to be perfect just print out the lyrics:

    PHP Code:
    $lyrics '99 bottles....';
    echo 
    $lyrics
    I'm assuming you want the code to generate it.
    Haha, that was a good one!

    Anyway, why is while nasty? I'm an absolute beginner so forgive me if its a stupid question.

    Thanks,
    Jaysone

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    How about simply this?
    PHP Code:
    $beer 99;
    while(
    $beer >= 0)  {
      if(
    $beer-== 0){
          echo(
    $beer.' bottles of beer on the wall, '.$beer.' bottles of beer,<br/>');
          echo(
    'Take one down and pass it around, no more bottles of beer on the wall.<br/>');
      }
      else if(
    $beer-0){
          echo(
    'No more bottles of beer on the wall, no more bottles of beer,<br/>');
          echo 
    'Go to the store and buy some more, 99 bottles of beer on the wall.';
      }
      else{
          echo(
    $beer.' bottles of beer on the wall, '.$beer.' bottles of beer,<br/>');
          echo(
    'Take one down and pass it around, '.(($beer == )).' bottles of beer on the wall.<br/>');
      }
      echo(
    '<br/>');
      
    $beer--;

    Not much hard tricks :P
    Thank you rajug, haven't tried it yet but once i've perfected it i'll post the final version here.

    Cheers,
    Jaysone


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •