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
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
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
Share on other sites

I automatically assumed that $a-1 would always output 99 for some reason.

Thanks very much for the replies, another lesson learned :D

 

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.

Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.