The PHP for() loop

And another loop for you to try is the for() loop. This repeats for a specified number of times.

$num = 10
for ($i=0; $i < $num; $i++) {
print $num;
print "<br />";
}

This will print out the numbers 0 to 9, each on a new line. With this loop we can also use it to combine variable variables to iterate through a series of repetitive form fields eg. name1, name2 and name3.

For example:

for ($i=1; $i < $num_fields; $i++) {
$temp = "name".$i;
print $$temp."<br />";
}

As you can see you create a dynamic variable name using the value of $i (1, 2, 3 etc) and then save this string in the variable $temp. Therefore on the first iteration $temp contains “name1”. To get this to display the content of $name1 you need to use two dollar signs to get the variable variable ie. $$temp. This is very handy when you do not know how many text boxes may be coming from a form, such as in an ecommerce shopping basket. This way if each box has a generic name plus a number on the end which increments each time, it is easy to pass through the count of fields from the form page (which is then stored as $num_fields) and then it can display the code. Plus imagine having 20 text boxes. Compare 20 lines of code to just 4!

Additional Note: To access the form fields you would use slightly different code as below

for ($i=1; $i < $num_fields; $i++) { print $_POST['name'.$i]."<br />"; }

You may also like...

14 Responses

  1. Grateful says:

    Thanks That was just what I was looking for!

  2. basic php learner says:

    This line:
    $_POST[‘name’.$i]
    saved my bacon. I couldn’t figure out how to cycle through my variable names!
    Thank you a million times!

  3. Con says:

    How can i save the variables into a text file?

  4. Sarah says:

    Hi Con, you need to use the fwrite() function.

  5. Mich says:

    Hi!

    Is it possible to give ‘$temp’ also a number?

    So that you have:

    $temp1 = “name”.$i;
    $temp2 = “name”.$i;
    $temp3 = “name”.$i;
    $temp4 = “name”.$i;

    I would like to use those variables also after the loop. 🙂

  6. Sarah says:

    Hi Mich,

    Yes you can. If you take the second code sample above but instead of giving $temp the value of “name”.$i you would use “temp”.$i and then assign $$temp the value of “name”.$i or whatever you wanted to assign to it.

    Then after the loop you’d have $temp1, $temp2 etc.

    eg.


    for ($i=1; $i < $num_fields; $i++) { $temp = "temp".$i; $$temp = "name".$i; }

    And then $temp1 equals name1, $temp2 equals name2 etc.

  7. Dan says:

    Hi Sarah

    first of all thanks for nice site.

    my question is, if it is possible access class variables with this method

    i tried:

    for($i=1; $i variable;

    }

    i don’t get anything echoed
    another this is, if the method you will post also can be used for class functions

    THANKS for you answer 😉

    • Sarah says:

      Hi Dan, I’m a bit lost on what you’re trying to do. The for loop should be written in the style of

      for ($i=1; $i<$x; $i++) {
      // put your loop code here
      }

      Where $x is the top limit. Of course you can modify these clauses to have something counting down instead of up etc. If that doesn’t answer your question I’ll need to know a bit more about what you’re trying to do.

  8. Dan says:

    i had some trouble with posting my code i tried several times until i just went with it… but htanks for your answer.

  9. Emil says:

    I have the following:

    for ( $i = 1; $iparent_cost$i”;
    $tcc = “this->child_cost$i”;
    $cst = “cost$i”;
    if ( $this->;parent_cost1 > $this->child_cost1 ) {
    $$cst = $$tpc;
    }
    else {
    $$cst = $$tcc;
    }
    echo ” cost: “.$cst;
    }

    I am at a loss as how to display the contents of $cst

  10. Emil says:

    Every time I tried to paste the code it came up garbled. But I was able to resolve it, wrong pointers.

    Thanks for your response.

  11. ANAT says:

    Thanks for “print $$temp.””
    Thanks for your answer

  12. Ryan Harne says:

    Thanks! it’s works well!

Leave a Reply

Your email address will not be published. Required fields are marked *