Programming in PHP - Control Structures (part 2)
Posted 02/04/2015 at 22:50 by manulaiko3.0
Copy and paste from my website: 
The last time we talk about control structures that choose between 1, 2 and more options, now we’re going to talk about loops.
What is a loop?
A loop is a piece of code that repeats X times, the times that the code is repeated is given by a condition, there are 2 (actually 4, I will explain 3 because I’ve never used the last one and I don’t like it) control structures that lets you loop through the code:
For
This is one of the most powerfull control structures (all the control structures are necessary and powerfull) and it looks like this:
As every other control structure it starts with the keyword for and is followed by 3 instructions between parenthesis:
Iterator declaration
This instruction can be omited, but I really recommend to don’t do it. Bassically you make a new variable and assings it a value.
The most used is an integer variable called i (short of iterator) and it starts in 0.
Condition
This is easy, it’s a comparation, the loop won’t stop until that comparation is true.
Iteration incrementation/decrementation
If the iterator never changes, then the condition will never be true, and that means that the loop won’t finish till the end of the universe (or the end of computer [soon if you’re running in a Slackware with 760MB ram and a Pentium 4 like me :P]), for that reason, the last instruction between the parenthesis is dedicated to change the iterator.
Here you bassically asing a new value based in the last value of the iterator (+1, -2, +4, *134…)
Let’s see how does this look in PHP:
<?php for($i = 0; $i < 10; $i++) { //Iterator declaration: $i = 0 //Condition: $i < 10; //Iterator incrementation: $i++, it's the same as $i = $i + 1 echo $i; } //The script should print "0123456789" And that’s all about the for loop
While
This is more simple than the for loop, it looks something like this:
This loop needs to declare the iterator before the while keyword (in the upper line) and must be modified inside its block, let’s look at the code:
<?php $i = 0; //declare the iterator while($i < 10) { echo $i; //Increment the iterator $i = $i + 1; //same as $i++ } //The script should print "0123456789" And that’s all about while loop. No more shit untill next chapter: Arrays
See you!

The last time we talk about control structures that choose between 1, 2 and more options, now we’re going to talk about loops.
What is a loop?
A loop is a piece of code that repeats X times, the times that the code is repeated is given by a condition, there are 2 (actually 4, I will explain 3 because I’ve never used the last one and I don’t like it) control structures that lets you loop through the code:
- For
- While
For
This is one of the most powerfull control structures (all the control structures are necessary and powerfull) and it looks like this:
for (iterator declaration; condition; iterator incrementation/decrementation)Well, lets explain this.
//do something..
As every other control structure it starts with the keyword for and is followed by 3 instructions between parenthesis:
- Iterator declaration
- Condition
- Iterator incrementation/decrementation
Iterator declaration
This instruction can be omited, but I really recommend to don’t do it. Bassically you make a new variable and assings it a value.
The most used is an integer variable called i (short of iterator) and it starts in 0.
Condition
This is easy, it’s a comparation, the loop won’t stop until that comparation is true.
Iteration incrementation/decrementation
If the iterator never changes, then the condition will never be true, and that means that the loop won’t finish till the end of the universe (or the end of computer [soon if you’re running in a Slackware with 760MB ram and a Pentium 4 like me :P]), for that reason, the last instruction between the parenthesis is dedicated to change the iterator.
Here you bassically asing a new value based in the last value of the iterator (+1, -2, +4, *134…)
Let’s see how does this look in PHP:
<?php for($i = 0; $i < 10; $i++) { //Iterator declaration: $i = 0 //Condition: $i < 10; //Iterator incrementation: $i++, it's the same as $i = $i + 1 echo $i; } //The script should print "0123456789" And that’s all about the for loop
While
This is more simple than the for loop, it looks something like this:
while (condition)The keyword is while and is followed by a condition between parenthesis, untill that condition isn’t true, the loop won’t end.
//do something…
This loop needs to declare the iterator before the while keyword (in the upper line) and must be modified inside its block, let’s look at the code:
<?php $i = 0; //declare the iterator while($i < 10) { echo $i; //Increment the iterator $i = $i + 1; //same as $i++ } //The script should print "0123456789" And that’s all about while loop. No more shit untill next chapter: Arrays
See you!
Total Comments 0






