Membership
Main Menu
Forum Boards
Stats
- 18 tutorials
- 72,338 members
- 696,855 forum posts
- 11 blog posts
Tutorials
PHP Loops
Views: 21568
Going Around In Circles
The Initialization
This is where the loop starts. We tell the "for" loop where to start by assigning the starting number to a variable $x. In this example, our starting number is represented by $start - what the user puts in from the form.
In a "for" loop, the initialization must always be a number. It doesn't have to be a whole number, or even positive, but it has to be a number, and it has to be initialized by assignment to a variable, so that PHP can keep track of counting each iteration of the loop. In other words, it has to have this basic format:
$x = $start;
The Condition
This is where we control how many times we want the loop to repeat, or iterate. A "for" loop counts from one number to some other number. This is where we tell PHP what that other number is. Without the condition, the loop will never stop repeating, and we would have what's called an infinite loop.
The condition must always check the initializing variable against a number value. It doesn't have to be a whole number, it can be positive or negative, but it has to be a number value. You can use any of the normal condition operators for the condition. Some basic formats (that are relevant to our example) for the condition are:
$x < $finish;
$x <= $finish;
$x != $finish;
