Vikings Dood
Vikings Dood

Reputation: 51

Output contents of for loop into array

for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
  {
       echo "the number " . $numbers1;
       echo "<br>";
  }

I have the practice for loop above. I need to return the data into an array and loop through and print the data to the page. I would GREATLY appreciate if you could break apart your answer with notations explaining this as I am trying to learn. Thanks!

Upvotes: 1

Views: 67

Answers (2)

Tns
Tns

Reputation: 410

/*
 * Initilalize variable with an empty array.
 * Array can contain variables of any type, even other arrays and grows in size automatically.
 * In PHP 5.4 and newer you can use `$data = [];` syntax
 */
$data = array();

/*
 * Set variable $numbers1 to 1, then check that its value less than or equal to 150 and then run code between { and }
 * After the very last line in the `{ }` block perform variable increment (++$numbers1) - add 1 to the previous value.
 * Now $numbers1 becomes equal to 2, then check that its value less than or equal to 150 and again.
 * Loop will iterate over and over untill $numbers1 become eqial to 151 and `$numbers1 <= 150` evaluates to false.
 * Then loop breaks and code after closind `}` will be executed.
 */
for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
{
    /*
     * Set variable $value to the string. String will be built from 3 parts 
     *  - "the number ", 
     *  - string representation of $numbers1 value and
     *  - literal string "<br/>\n"
     *
     * "\n" means 'new line' symbol, in HTML is transforms into the space, but it will help you to debug your application -
     * you can see resulting code more structured in "view-source" mode of your browser (Ctrl+U in Firefox).
     * You can safely remove it.
     */
    $value = "the number " . $numbers1 . "<br/>\n";

    /**
     * Then add $value to the array. It is equivalent to array_push($data, $value)
     * String 'the number 1<br/>' becomes the first element (at 0 index, try to `echo $data[0];` somewhere),
     * string 'the number 2<br/>' becomes the second one ($data[1]) and so on.
     */
    $data[] = $value;
}

/* Tt is good habit to unset variables that you don not need anymore.
 * Try to `echo $value` and you will got Notice - variable does not exist.
 */
unset ($value, $numbers1);

/*
 * For page output you should use some kind of templating engine (Twig, for example). For now, we will use PHP itself
 */

// After this line starts plain HTML, PHP engine will output is as is. Like web server usially does.
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Demo!</title>
    </head>
    <body>
        <p>
            <?php 
                /*
                 * Meet PHP code again.
                 * All that printed or echoed will be put in place of the code between open and closing php tags
                 */
                foreach ($data as $value) {
                    echo $value;
                }
            ?>
        </p>
    </body>
</html>

Upvotes: 1

user6655061
user6655061

Reputation: 92

$numbers_array = array();

for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
  {
       array_push($numbers_array,$numbers1);//add the number to the array
  }

print_r($numbers_array);//print out the array

If you want to only print the values, you can add this line to the for loop:

var_dump($numbers_array[$numbers1-1]);//prints out the element in the position that is one less then the number you want to print out (since arrays in php are zero based)

Upvotes: 2

Related Questions