Lee Posted March 14, 2007 Share Posted March 14, 2007 Hi, this is a boring noob question. I'm practicing loops & tried the following code but it outputs in an odd way, I just want to see where I have gone wrong. Thanks <?php $start = 'green bottles hanging on the wall'; $and = 'and if one'; $fall = 'green bottle should accidentally fall, there\'ll be'; $end = 'green bottles left on the wall'; ?> <?php for ($a = 100; $a >= 1; $a--) for ($b = 99; $b >=1; $b--) echo "$a $start $and $fall $b $end <br>" ?> Quote Link to comment Share on other sites More sharing options...
papaface Posted March 14, 2007 Share Posted March 14, 2007 You need your {}'s I think it should be: <?php $start = 'green bottles hanging on the wall'; $and = 'and if one'; $fall = 'green bottle should accidentally fall, there\'ll be'; $end = 'green bottles left on the wall'; ?> <?php for ($a = 100; $a >= 1; $a--) { for ($b = 99; $b >=1; $b--) { echo "$a $start $and $fall $b $end <br>" } } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 14, 2007 Share Posted March 14, 2007 You don't need the curly braces if there is only one statement in the block. I believe you have too many loops, remove the second loop: <?php $start = 'green bottles hanging on the wall'; $and = 'and if one'; $fall = "green bottle should accidentally fall, there'll be"; $end = 'green bottles left on the wall'; for ($a = 100; $a >= 1; $a--) echo "$a $start $and $fall " . ($a - 1) . " $end <br>" ?> Ken Quote Link to comment Share on other sites More sharing options...
Lee Posted March 14, 2007 Author Share Posted March 14, 2007 I automatically assumed that $a-1 would always output 99 for some reason. Thanks very much for the replies, another lesson learned Is it ok to ask these types of noob questions in here? Some things I am learning from books & videos confuse me a bit. Thanks again. Quote Link to comment Share on other sites More sharing options...
Lee Posted March 15, 2007 Author Share Posted March 15, 2007 I can't believe it, I tried doing the same thing using an array & it worked first time! I'm so happy with myself I had to tell someone & my missus just wouldn't understand what the hell I'm on about So here's what I wrote, is there a better/alternative way to do this array? <?php $ar_rhyme = array ('green bottles hanging on the wall ','and if one ', 'green bottle should accidentally fall, there\'ll be ', 'green bottles left on the wall.'); for ($a = 100; $a >= 1; $a--) echo "$a"." $ar_rhyme[0]"."$ar_rhyme[1]"."$ar_rhyme[2]".($a-1)." $ar_rhyme[3] <br>" ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 15, 2007 Share Posted March 15, 2007 You don't have to put the text into variables or an array, since the text is static, just echo it out along with the variable: <?php for ($a = 100; $a >= 1; $a--) echo $a . " green bottles hanging on the wall and if one green bottle should accidentally fall, there'll be " . ($a - 1) . ' green bottles left on the wall.<br>'; ?> Ken Quote Link to comment Share on other sites More sharing options...
Lee Posted March 15, 2007 Author Share Posted March 15, 2007 Thanks Ken. I just wanted to understand how to make the program work using variables & arrays. I just used this text to practice using them in different ways. Does anyone know of any other simple programs like this to practice with? Or slightly more progressive? Next I want to ty to do it using strtolower & ucwords functions, but I'm not sure where & how to format/write the function. Thanks for your help guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.