How to resolve the algorithm 99 bottles of beer step by step in the PHP programming language
How to resolve the algorithm 99 bottles of beer step by step in the PHP programming language
Table of Contents
Problem Statement
Display the complete lyrics for the song: 99 Bottles of Beer on the Wall.
The lyrics follow this form: ... and so on, until reaching 0 (zero). Grammatical support for 1 bottle of beer is optional. As with any puzzle, try to do it in as creative/concise/comical a way as possible (simple, obvious solutions allowed, too).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 99 bottles of beer step by step in the PHP programming language
First Source Code:
The first source code is a simple PHP program that prints the lyrics to the song "99 Bottles of Beer on the Wall". The program uses a foreach
loop to iterate over the numbers from 99 to 1, and for each number, it prints the corresponding verse of the song. The program uses the $plural
variable to keep track of whether the word "bottle" should be singular or plural, and it uses the if
statement to change the value of $plural
when the number of bottles changes.
Second Source Code:
The second source code is another PHP program that prints the lyrics to the song "99 Bottles of Beer on the Wall". This program uses a foreach
loop to iterate over the numbers from 99 to 1, and for each number, it prints the corresponding verse of the song using a <<<EOV
heredoc. The program uses the $p
variable to keep track of whether the word "bottle" should be singular or plural, and it uses the if
statement to change the value of $p
when the number of bottles changes.
Third Source Code:
The third source code is a PHP program that prints the lyrics to the song "99 Bottles of Beer on the Wall" using a foreach
loop and a <?=$i?>
echo statement for each number from 99 to 1. The program uses a ternary operator to determine whether the word "bottle" should be singular or plural, and it uses the if
statement to change the value of $i
when the number of bottles changes.
Fourth Source Code:
The fourth source code is a PHP program that prints the lyrics to the song "99 Bottles of Beer on the Wall" using a foreach
loop and a $verse
variable to store the lyrics. The program uses a preg_replace
function to replace the numbers in the $verse
variable with the numbers from 1 to 99. The program also uses the $verse
variable to store the lyrics for the final verse of the song.
Fifth Source Code:
The fifth source code is a PHP program that prints the lyrics to the song "99 Bottles of Beer on the Wall" using a for
loop and a $p2
variable to store the lyrics for each verse. The program uses the $p1
variable to store the lyrics for the first line of each verse, and the $p3
variable to store the lyrics for the third line of each verse. The program uses the if
statement to change the value of $p1
when the number of bottles changes.
Sixth Source Code:
The sixth source code is a PHP program that prints the lyrics to the song "99 Bottles of Beer on the Wall" using a while
loop and a $bottles
variable to store the number of bottles. The program uses the printf
function to print the lyrics for each verse of the song, and it uses the ngettext
function to determine whether the word "bottle" should be singular or plural. The program also uses the if
statement to change the value of $bottles
when the number of bottles changes.
Seventh Source Code:
The seventh source code is a PHP program that prints the lyrics to the song "99 Bottles of Beer on the Wall" using a while
loop and a $lyrics
variable to store the lyrics. The program uses the printf
function to print the lyrics for each verse of the song, and it uses the sprintf
function to format the lyrics for the final verse of the song. The program also uses the $x
variable to store the number of bottles, and it uses the if
statement to change the value of $x
when the number of bottles changes.
Source code in the php programming language
<?php
$plural = 's';
foreach (range(99, 1) as $i) {
echo "$i bottle$plural of beer on the wall,\n";
echo "$i bottle$plural of beer!\n";
echo "Take one down, pass it around!\n";
if ($i - 1 == 1)
$plural = '';
if ($i > 1)
echo ($i - 1) . " bottle$plural of beer on the wall!\n\n";
else
echo "No more bottles of beer on the wall!\n";
}
?>
<?php
foreach(range(99,1) as $i) {
$p = ($i>1)?"s":"";
echo <<< EOV
$i bottle$p of beer on the wall
$i bottle$p of beer
Take one down, pass it around
EOV;
}
echo "No more Bottles of beer on the wall";
?>
<?php foreach(range(99,1) as $i):?>
<?=$i?> bottle<?=$i==1 ? '' : 's'?> of beer on the wall,
<?=$i?> bottle<?=$i==1 ? '' : 's'?> of beer!
Take one down, pass it around...
<?php if($i > 1):?>
<?=$i-1?> bottle<?=$i==1 ? '' : 's'?> of beer on the wall!
<?php else:?>
No more bottles of beer on the wall!
<?php endif?>
<?php endforeach?>
<?php
$verse = <<<VERSE
100 bottles of beer on the wall,
100 bottles of beer!
Take one down, pass it around!
99 bottles of beer on the wall!
VERSE;
foreach (range(1,99) as $i) { // loop 99 times
$verse = preg_replace('/\d+/e', '$0 - 1', $verse);
$verse = preg_replace('/\b1 bottles/', '1 bottle', $verse);
$verse = preg_replace('/\b0 bottle/', 'No bottles', $verse);
echo $verse;
}
?>
<?php
for($i=100;$i>0;$i--){
$p2=$i." bottle".(($i>1)?"s":"")." of beer";
$p1=$p2." on the wall\n";
$p3="Take one down, pass it around\n";
echo (($i<100)?$p1."\n":"").$p1.$p2."\n".$p3.(($i<2)?($i-1).substr($p1,1,28):"");
}
<?php
$bottles = 99;
while ($bottles > 0) {
printf(ngettext('%d bottle', '%d bottles', $bottles) . " of beer on the wall\n", $bottles); //X bottles of beer on the wall
printf(ngettext('%d bottle', '%d bottles', $bottles) . " of beer\n", $bottles); //X bottles of beer
printf("Take one down, pass it around\n"); //Take one down, pass it around
$bottles--;
if ($bottles > 0) {
printf(ngettext('%d bottle', '%d bottles', $bottles) . " of beer on the wall\n\n", $bottles); //X bottles of beer on the wall
}
}
printf('No more bottles of beer on the wall'); //No more bottles of beer on the wall
<?php
$lyrics = <<<ENDVERSE
%2\$d bottle%1\$s of beer on the wall
%2\$d bottle%1\$s of beer
Take one down, pass it around
%4\$s bottle%3\$s of beer on the wall
ENDVERSE;
$x = 99;
while ( $x > 0 ) {
printf( $lyrics, $x != 1 ? 's' : '', $x--, $x != 1 ? 's' : '', $x > 0 ? $x : 'No more' );
}
You may also check:How to resolve the algorithm First-class functions/Use numbers analogously step by step in the TXR programming language
You may also check:How to resolve the algorithm Feigenbaum constant calculation step by step in the RPL programming language
You may also check:How to resolve the algorithm Solve a Hidato puzzle step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Sub-unit squares step by step in the Perl programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the EchoLisp programming language