Solve this PHP puzzle in as few bytes as possible

Frozen69

New Member
*EDIT: since some folk's panties are apparently bunching uncomfortably over this, how about just recommending some efficient ways of dealing with similar code? Maybe not specific answers to the problem I posed (my goal anyway, this bit of code golf was merely a means to finding more concise ways of dealing with the basic problems presented), rather, general suggestions for ways to optimize/shorten things dealt with by this question? As I said...This is just meant to be fun (and if you are a l337 programmer who is offended by such noobish questions... well, tough)!I found a PHP programming puzzle online that asks you to solve a problem as efficiently as possible. The problem is to print the "99 Bottles of Beer on the Wall" song using as little code as possible. The required lines are:\[quote\] 99 bottles of beer on the wall, 99 bottles of beer. Take 1 down, pass it around, 98 bottles of beer on the wall. 98 bottles of beer on the wall, 98 bottles of beer. Take 1 down, pass it around, 97 bottles of beer on the wall.\[/quote\]etc, etc, on down to...\[quote\] 1 bottle of beer on the wall, 1 bottle of beer. Go to the store and buy some more, 99 bottles of beer on the wall.\[/quote\]The best I could manage was not very good compared to some other solutions I found. Basically, you want to solve it in the fewest bytes possible. I did it in about 260 bytes. I saw someone else that did it in a mere 187. What I'm interested in finding out here is... how did he/she do it?! I will print my solution here for reference and to prove that I did it (and I'm not asking for a homework solution, lol) but I am very curious as to how one would optimize the solution to use as few bytes as possible (fyi my solution could shave a few bytes by not using newlines but I want it to be as readable as possible here).\[code\]<?php$a="beer on the wall";for($i=99;$i>=1;$i--){$x=($i==1)?"bottle":"bottles";$b="<p>$i $x of $a, $i $x of beer.<br>";$b.=($i==1)?"Go to the store and buy some more, 99 bottles of $a.":"Take one down and pass it around, ".($i-1)." $x of $a.</p>";echo $b;}\[/code\]Oh, you can get the number of bytes by creating the file and then running:\[code\]ls -l /location/of/the/dir/containing/the/file/\[/code\]
 
Back
Top