Hello Everyone,

Glad to have found this forum. Hopefully someone might be able to help me see the light with this project I must do. I am taking a PHP class. My assignment is to create an array and fill it with numbers from 1 to 100 and then get it to print to the screen using a while statement.

While I am now four hours into this and can not figure it out here is what I have if anyone would like to shed some light on my error and point me in the right direction that would be so kind! I have read my book and tutorials, at this point I feel like all I am doing is confusing myself more.....Errr!

<?php
$Count=1;
while ($Count >100){
$Numbers= array ("$Count");
++$Count;
foreach ($Count as $CumNum)
echo"<p>$CumNum</p>";
}
?>

Thanks :o

    Without just giving you the answer, some things to consider:

    • There are two basic ways to assign values to an array. The method you used with the array() function will overwrite the variable it's assigned to with the values listed in the parens, so that's not going to work this way. Read through this page for the alternate "brackets" method.

    • While you could do what you are doing with the while loop, you'll need to invert your ">" comparison to a "<=" comparison. However, I'd recommend that a for loop would be a bit cleaner and more appropriate.

    • And, once you've worked all that out and learned some very useful basic programming knowledge, take a look at PHP's built-in [man]range/man function. 🙂

      Thanks!

      I will study the for statement and give it a spin and let you know how I did.

      Cheers!

        Socalgal wrote:

        Hello Everyone,

        Glad to have found this forum. Hopefully someone might be able to help me see the light with this project I must do. I am taking a PHP class. My assignment is to create an array and fill it with numbers from 1 to 100 and then get it to print to the screen using a while statement.

        While I am now four hours into this and can not figure it out here is what I have if anyone would like to shed some light on my error and point me in the right direction that would be so kind! I have read my book and tutorials, at this point I feel like all I am doing is confusing myself more.....Errr!

        <?php

        $Count=1;
        while ($Count >100){
        $Numbers= array ("$Count");
        ++$Count;
        foreach ($Count as $CumNum)
        echo"<p>$CumNum</p>";
        }
        ?>

        Thanks :o

        To answer your question :

        <?php
        
        $Count=0;
        $array1 = array();
        
        while ($Count < 100)
        {
        $array1[$Count] = $Count; // e.g. array1[1] = 1
        ++$Count;
        }
        
        foreach ($array1 as $CumNum) // you wrote for each $count, it should be for each of the array contents
        { 
        echo "<p>$CumNum</p>"; }
        
        ?>
        
        
        // for loop
        
        <?php
        $array1 = array();
        
        for($x=0; $x<101; $x++)
        {
        	$array1[$x] = $x;	
        }
        
        foreach ($array1 as $CumNum)
        {
        	echo "<p>$CumNum</p>"; 
        }
        
        ?>
        

          Socalgal, attempt the problem again yourself before looking at naskar's solution. Anyway, naskar's solution is a negative example in the sense that it lacks proper indentation and still uses a while loop instead of a for loop, but a positive example in that it is posted within php bbcode tags (which you should also use when posting PHP code here).

            Hello Laserlight,

            I will try it again. I will also moving forward put tags to put my code in the box.

            Thank you all for being so helpful and patient with me.

            Cheers!

              I will try it again. I will also moving forward put tags to put my code in the box.

              That's good. Remember, to learn, you need to practice, so do not be afraid to try before reading a "model answer".

              In an attempt to provide a better "model answer", this would be my solution:

              <?php
              $numbers = array();
              
              // fill array with numbers from 1 to 100
              for ($i = 1; $i <= 100; ++$i)
              {
                  $numbers[] = $i;
              }
              
              // print array using a while statement
              while (list($key, $value) = each($numbers))
              {
                  echo $value . ' ';
              }
              ?>

              Notice how I populated the array - it is a numerically indexed array since I let PHP calculate the index at which to append new elements with "$numbers[] = $i" instead of "$numbers[$i] = $i". Note also the for loop.

              For printing, I chose to use [man]list/man and [man]each/man. Basically, each() returns the current key/element pair from the array and advances the internal array pointer/cursor, and list() assigns the key to $key and the element to $value. If you did not have the restriction of using a while loop, you would use:

              // print array
              foreach ($numbers as $key => $value)
              {
                  echo $value . ' ';
              }

              or better yet, since you do not need the key:

              // print array
              foreach ($numbers as $value)
              {
                  echo $value . ' ';
              }

              EDIT:
              Incidentally, if you had no restrictions at all, you could just use [man]range/man and [man]implode/man, e.g.,

              <?php
              // fill array with numbers from 1 to 100
              $numbers = range(1, 100);
              
              // print array
              echo implode(' ', $numbers);
              ?>

              Of course, this would not teach you about loops, but about range() and implode().

                Thank you all I took NogDog advice and changed it to a for statement. Then I realized my error after reading Naskar entry. LaserLight I will study what you have add for my knowledge.

                I understand how important it is to fully get looping statments.

                Thank you one and all.

                I checked the box for problem solved (I think?.....Ok must go study now!)

                  Write a Reply...