Language PHP
(Configurable through URL params)
| Date: | 07/25/08 |
| Author: | James Wright |
| URL: | n/a |
| Comments: | 8 |
| 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--
":""
"