Forum Moderators: coopster

Message Too Old, No Replies

for loops

Explanation of the for control structure

         

Kysmiley

9:48 pm on Oct 17, 2004 (gmt 0)

10+ Year Member



I'm trying to0 learn PHP and keep running into what looks like an example. I'm not sure though if the $i is suppose to mean somthing of if it is just an example. Here is the common thing I am seeing
for ($i o; $i<3; $i++ )
So am I correct in thinking this just an example that this author uses over and over for incrementing, or is it a predefined variable in PHP

coho75

11:10 pm on Oct 17, 2004 (gmt 0)

10+ Year Member



You are correct in your thinking. This is an example of incrementing a variable.

Russ49Checkmate

4:56 am on Oct 18, 2004 (gmt 0)

10+ Year Member



$i is just an example. This can be any available variable. Many others use $n for this type of incrementing.

dreamcatcher

9:17 am on Oct 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The $i increments inside the loop, so if you have:

for ($i=0; $i<3; $i++ )
{
echo $i;
}

Then you will see a display of 012. What the loop is saying is $i starts as 0, then so long as the value of $i is less than 3, it will continue looping. The start value is set at 0, so the first increment will be this value. Once it reaches 2, it stops because the next value (3) would be false. The $i++ is the same as doing $i=$i+1.

Its quite popular in most (well, probably all) programming languages.