EvilZone

Programming and Scripting => Web Oriented Coding => : Rytiou October 20, 2013, 03:11:49 PM

: PHP - 99 Bottles of Beer Program
: Rytiou October 20, 2013, 03:11:49 PM
So I've been learning php for a little bit and I decided I wanted to make a simple program to test what I learnt. I know this is nothing exciting really but I wanted to make something simple from scratch without a book telling me what to do. If you can tell me how I did on this 99 bottles of beer program I'd really appreciate it :).


:
<?php


// Sets the initial amount of beer
$count 99;


// Prints the lyrics while count is greater than or equal to 1
while ($count >= 1)
{
echo $count.' bottles of beer on the wall, '.$count.' bottles of beer. <br>';
echo 'Take one down and pass it around, '.($count 1).' bottles of beer on the wall';
echo '<br>';

// Decreases count by one
$count --;
}


?>
: Re: PHP - 99 Bottles of Beer Program
: Fur October 20, 2013, 03:54:29 PM
Not bad, could use pre-decrement though:
:
echo 'Take one down and pass it around, ' . --$count . ' bottles of beer on the wall';
echo '<br>';
: Re: PHP - 99 Bottles of Beer Program
: Rytiou October 20, 2013, 04:17:55 PM
Well  I tried doing that but what ended up happening was the number was decreasing by two rather than one. And when I remove the bottom one but keep the one you put in it does this:

http://gyazo.com/954cc33babf58434114597c2cd910905
: Re: PHP - 99 Bottles of Beer Program
: ande October 20, 2013, 05:32:08 PM
Isen't the point of the 99-bottles-of-beer song in code to make it as small as possible? Here is what I got on short notice:

[gist]anonymous/7071149[/gist]
: Re: PHP - 99 Bottles of Beer Program
: Rytiou October 20, 2013, 05:55:06 PM
Isen't the point of the 99-bottles-of-beer song in code to make it as small as possible? Here is what I got on short notice:

[gist]anonymous/7071149[/gist]
Well this was the shortest possible way that I could think of with my current knowledge. Also, if you're trying to show me an example it's either not there or it's just my browser.
: Re: PHP - 99 Bottles of Beer Program
: ande October 20, 2013, 06:57:45 PM
Well this was the shortest possible way that I could think of with my current knowledge. Also, if you're trying to show me an example it's either not there or it's just my browser.

You need to enable javascript. Jesus, what is it with people and having javascript disabled..


EDIT: Direct link: https://gist.github.com/anonymous/7071149#file-gistfile1-php
: Re: PHP - 99 Bottles of Beer Program
: vezzy October 20, 2013, 06:59:16 PM
You need to enable javascript. Jesus, what is it with people and having javascript disabled..

Pretty good security practice, if you ask me.
: Re: PHP - 99 Bottles of Beer Program
: Rytiou October 20, 2013, 07:11:44 PM
You need to enable javascript. Jesus, what is it with people and having javascript disabled..


EDIT: Direct link: https://gist.github.com/anonymous/7071149#file-gistfile1-php
Aah didn't even think about that. Guess I still have a bunch to learn. Thanks for showing me man :).
: Re: PHP - 99 Bottles of Beer Program
: ande October 20, 2013, 07:18:57 PM
Pretty good security practice, if you ask me.

Globally disabling javascript does more harm than good if you ask me. If you need to disable javascript to be secure while browsing, you should consider adapting a new browsing habit.
: Re: PHP - 99 Bottles of Beer Program
: Rytiou October 20, 2013, 07:20:15 PM
Globally disabling javascript does more harm than good if you ask me. If you need to disable javascript to be secure while browsing, you should consider adapting a new browsing habit.
Well the only reason I had disabled was because I was testing a firefox addon and I forgot that I had it on still.
: Re: PHP - 99 Bottles of Beer Program
: Deque October 20, 2013, 09:20:06 PM
The very last verse will be grammatically wrong, won't it?
: Re: PHP - 99 Bottles of Beer Program
: ande October 20, 2013, 11:50:15 PM
The very last verse will be grammatically wrong, won't it?

Indeed, needs to say bottle not bottles. Should be simple enough.
: Re: PHP - 99 Bottles of Beer Program
: BrokenSyntax October 26, 2013, 10:57:39 PM
:
for ($i = 100; $i > 0; $i--) {
   echo sprintf('%1$d %3$s of beer on the wall, %1$d %3$s of beer.<br>Take one down and pass it around, %2$d %4$s of beer on the wall<br>', $i, ($i-1), ($i==1) ? 'bottle' : 'bottles', (($i-1)==1) ? 'bottle' : 'bottles');
}

This will, to my knowledge, fix the grammar in the last verse