Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language PHP

(Configurable through URL params)

Date:07/25/08
Author:James Wright
URL:n/a
Comments:11
Info:http://php.net/
Score: (2.85 in 47 votes)
<?
	/* CONFIG FROM URL STRING, DEFAULT TO 99 BOTTLES OF BEER ON THE WALL*/
	if($_REQUEST['bottleNumber']) { $bottleNumber = $_REQUEST['bottleNumber']; } else { $bottleNumber =
99; }
	if($_REQUEST['fluid']) { $fluid = $_REQUEST['fluid']; } else { $fluid = 'beer'; }
	if($_REQUEST['location']) { $location = $_REQUEST['location']; } else { $location = 'wall'; }

	/* CREATE LYRICS */
	for($i = $bottleNumber; $i >= 0; $i--) {

		/* SET DEFAULTS */
		$bottleCount = $i;
		$bottle = 'bottles';
		$action = 'Take one down and pass it around';
		$remainingBottles = $i-1;

		/* HANDLE EXCEPTIONS */
		if($i == 0) {
			$bottleCount = 'no more';
			$action = 'Go to the store and buy some more';
			$remainingBottles = $bottleNumber;
		}
		if($i == 1) {
			$bottle = 'bottle';
			$action = 'Take it down and pass it around';
			$remainingBottles = 'no more';
		}

		/* BUILD LYRICS */
		$lyrics .= ucfirst($bottleCount).' '.$bottle.' of '.$fluid.' on the '.$location.',
'.$bottleCount.' '.$bottle.' of '.$fluid.'.<br>'."\n";
		$lyrics .= $action.', '.$remainingBottles.' bottles of '.$fluid.' on the '.$location.'.<p
\>'."\n\n";
	}

	/* OUTPUT TO PAGE */
	echo $lyrics;
?>

Download Source | Write Comment

Alternative Versions

Comments

>>  Dan Dresselhaus said on 09/08/08 00:47:49

Dan Dresselhaus Third to last verse reads 'Take one down pass it around, 1 bottles of beer on the wall' This is an additional exception that needs to be handled.

<?
/* CONFIG FROM URL STRING, DEFAULT TO 99 BOTTLES OF BEER ON THE WALL*/
if($_REQUEST['bottleNumber']) { $bottleNumber = $_REQUEST['bottleNumber']; } else { $bottleNumber
= 99; }
if($_REQUEST['fluid']) { $fluid = $_REQUEST['fluid']; } else { $fluid = 'beer'; }
if($_REQUEST['location']) { $location = $_REQUEST['location']; } else { $location = 'wall'; }

/* CREATE LYRICS */
for($i = $bottleNumber; $i >= 0; $i--) {

/* SET DEFAULTS */
$bottleCount = $i;
$bottle = 'bottles';
$bottle2 = 'bottles';
$action = 'Take one down and pass it around';
$remainingBottles = $i-1;

/* HANDLE EXCEPTIONS */
if($i == 0)
{
$bottleCount = 'no more';
$action = 'Go to the store and buy some more';
$remainingBottles = $bottleNumber;
}
if($i == 1)
{
$bottle = 'bottle';
$action = 'Take it down and pass it around';
$remainingBottles = 'no more';
}
if($remainingBottles == 1)
{
$bottle2 = 'bottle';
}

/* BUILD LYRICS */
$lyrics .= ucfirst($bottleCount).' '.$bottle.' of '.$fluid.' on the '.$location.',
'.$bottleCount.' '.$bottle.' of '.$fluid.'.<br>'."\n";
$lyrics .= $action.', '.$remainingBottles.' '.$bottle2.' of '.$fluid.' on the '.$location.'.<p
\>'."\n\n";
}

/* OUTPUT TO PAGE */
echo $lyrics;
?>

>>  Mr. PHP said on 10/21/08 23:22:52

Mr. PHP Shorter. Faster. Less RAM usage. Oh, and still the same effect...

<?php
if(!$_GET["bottleNumber"]) $_GET["bottleNumber"] = 99;
if(!$_GET["fluid"]) $_GET["fluid"] = "beer"; else $_GET["fluid"] = htmlspecialchars($_GET["fluid"]);
if(!$_GET["location"]) $_GET["location"] = "wall"; else $_GET["location"] = htmlspecialchars($_GET["location"]);

for($i = (int) $_GET["bottleNumber"]; $i > 1; $i--)
echo $i . "bottles of " . $_GET["fluid"] . " on the " . $_GET["location"] . ", " . $i . " bottles of " . $_GET["fluid"] . ".\n" .
"Take one down and pass it around, " . ($i - 1) . " bottles of " . $_GET["fluid"] . " on the " . $_GET["location"] . ".\n\n";

echo "1 bottle of " . $_GET["fluid"] . " on the " . $_GET["location"] . ", 1 bottle of " . $_GET["fluid"] . ".\n" .
"Take one down and pass it around, no more bottles of " . $_GET["fluid"] . " on the " . $_GET["location"] . ".\n\n" .
"No more bottles of " . $_GET["fluid"] . " on the " . $_GET["location"] . ", no more bottles of " . $_GET["fluid"] . ".\n" .
"Go to the store and buy some more, 99 bottles of " . $_GET["fluid"] . " on the " . $_GET["location"];
?>

>>  Padde said on 01/22/09 21:30:16

Padde My opinion is, that the strength of PHP is its ease of use and so this is my version (i know that the purpose of this page/project is to show the features of an programming language, but i think that the feature of PHP actually IS its simplicity):

<?

for ($i=99; $i>=0; $i--)
{
if ($i==0) printf ("No more bottles of beer on the wall, no more bottles of beer.<br>
Go to the store and buy some more, 99 bottles of beer on the wall.<br><br>";);
elseif ($i==1) printf ("1 bottle of beer on the wall, 1 bottle of beer.<br>
Take one down and pass it around, no more bottles of beer on the wall.<br><br>";);
else printf ($i." bottles of beer on the wall, ".$i." bottles of beer.<br>
Take one down and pass it around, ".($i-1)." bottles of beer on the wall.<br><br>";);
}

?>

>>  foobarph said on 04/28/09 03:15:16

foobarph Thanks to James and Dan for the inspiration. Honestly, I'm sure I can still improve this code. If you have any further suggestions, feel free to email me. Thanks!

<?php

/* 99 bottles of beer by foobarph (http://rllqph.wordpress.com) */
for ($i = 99; $i >= 0; $i--) {

$bottles = $i;
$usedBottles = $i-1;

if ($i == 0) {
$action = "Go to the store and buy some more, ";
$sBottles = "bottle";
$bottles = "no more";
}

if ($i == 1) {
$sBottles = "bottle";
}

if ($i > 1) {
$action = "Take one down and pass it around, ";
$sBottles = "bottles";
}

if ($usedBottles == 0) {
$usedBottles = "no more";
}

if ($usedBottles < 0) {
$usedBottles = "99";
$sBottles = "bottles";
}

echo ucfirst($bottles) . " ". $sBottles ." of beer on the wall, ". $bottles ." ". $sBottles ." of beer.<br />";
echo $action . $usedBottles ." ". $sBottles ." of beer on the wall.<br /><br />";
}

?>

>>  Josha said on 05/08/09 16:09:24

Josha I think, this could be written muuuch shorter

>>  ioan1k said on 05/14/09 16:03:50

ioan1k 5 Lines......

for ($i = 1; $i <= 99; $i++) {
$first = ($i != 99) ? 99 - ($i - 1).' bottles of beer ' : ' 1 bottle of beer ';
$last = ($i == 99) ? ' no more bottles ' : (($i == 98) ? '1 bottle ' : 99 - $i.' bottles ');
echo sprintf('%s on the wall %s <br /> Take one down pass it around of %s beer on the wall <br />', $first, $first, $last);
}

>>  &#1042;&#1080;&#1088;&#1090;&# said on 07/02/09 01:34:11

&#1042;&#1080;&#1088;&#1090;&# Shortest version possible. Written about 3 years ago.

<?for($i=100;$i--;){$a=($i?$i:"no more";).$b=" bottl".($i-1?es:e)." of beer";$w=" on the wall";echo ($z++?"$a$w.

":"";),ucfirst("$a$w, $a.
";),$i?"Take one down and pass it around, ":Go;}echo " to the store and buy some more, 99$b$w.";

>>  Gang Reen said on 09/30/09 19:14:20

Gang Reen Don't want no short dick man. Don't want no short dick man. No no no no no short dick man.

>>  John said on 03/22/10 02:38:11

John Don't forget to sanitize!

>>  John said on 09/13/10 09:19:01

John How about one that prints nicely to web and cli, shares reusable code, is readable, and pays attention to the lyric's details (not always "take one down";),

<?php

$lineSep = @$_SERVER['argc'] >= 1 ? "\n" : "<br/>\n";

function beerStr($cnt) {
switch($cnt) {
case -1:
return '99 bottles of beer';
case 0:
return 'no more bottles of beer';
case 1:
return '1 bottle of beer';
default:
return "$cnt bottles of beer";
}
}

for ($i = 99; $i >= 0; $i --) {
$beerStr = beerStr($i);
$downBeerStr = beerStr($i-1);
$downStr = $i > 0 ? 'Take one down and pass it around' : 'Go to the store and buy some more';

echo ucfirst($beerStr)." on the wall, $beerStr.$lineSep"
. "$downStr, $downBeerStr on the wall.$lineSep$lineSep";
}

>>  barrym said on 09/14/10 06:03:50

barrym I'm no PHP expert, but it looks like at least a couple of these code comments are
deserving of "alternate versions" status. I suggest that you coders find a tag
that sets your code apart from the others and formally submit it. If you are the
impatient type like me, but want your code to look better in the comment section,
place a [ c o d e ] (without the spaces and on it's own line) before and
a [ / c o d e ] after, and your code sample will stand out nicely with most of
it's formatting preserved :-)

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: