Jump to content

Simple loop program


Lee

Recommended Posts

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>"

?>

Link to comment
https://forums.phpfreaks.com/topic/42706-simple-loop-program/
Share on other sites

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>"
  }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/42706-simple-loop-program/#findComment-207203
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/42706-simple-loop-program/#findComment-207212
Share on other sites

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 :D

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>"


?>

Link to comment
https://forums.phpfreaks.com/topic/42706-simple-loop-program/#findComment-207883
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/42706-simple-loop-program/#findComment-207950
Share on other sites

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 :D

Link to comment
https://forums.phpfreaks.com/topic/42706-simple-loop-program/#findComment-208002
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.