Language PHP
(Configurable through URL params)
Date: | 07/25/08 |
Author: | James Wright |
URL: | n/a |
Comments: | 11 |
Info: | http://php.net/ |
Score: | ![]() |
<? /* 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
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!
Comments
<?
/* 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;
?>
<?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"];
?>
<?
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>"
}
?>
<?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 />";
}
?>
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);
}
<?for($i=100;$i--
":""
"
<?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";
}
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