Monday 2 July 2012

All about PHP loops

So maybe you read my last post about arrays here, or maybe not. If you want a quick catch up or to learn something new check it out.

Quick recap. I'm a web developer at Computer Related. And I love web developing.

This post is about loops in PHP, it's not designed to be a be all you need post, but more of a reminder or a basic lesson to get you going.


I have many more tutorials and self help pages over at teachme.webege.com.


What is a loop?

A loop is exactly what it sounds like, something going around and around. For our purpose a loop is something that gets executed over and over again


Why do I need a loop

If you want to do anything more than once then more than likely you need a loop of some kind (not all the time).


Types of loops

In PHP we have four type of loops. We have a FOR loop, FOREACH loop, WHILE loop and a DO WHILE loop

.

How do I know which one to use

Once you understand the principles of a loop you'll know, which we'll go through next.


The principles

Imagine the scenario below

I want to print 10 numbers on seperate lines to the screen. And I may want to change that to 100 in the future.

You could just write 10 numbers in html, and if needed write another 90. But what if we then wanted 1000, this becomes more tedious. Sounds like a loop is needed.

What about if I created a loop that went round the amount of times i wanted, and each time it printed out a number to the screen. Now something like this could work with any amount of numbers.

Ok, lets do it.


for(i = 1; i <= 10; i++){
 echo i."/n";
}

Output =
1
2
3
4
5
6
7
8
9
10


i=1;
while(i <= 10){
 echo i."/n";
    i++;
}

Output =
1
2
3
4
5
6
7
8
9
10


do{
 i=1;
 echo i."/n";
    i++;
}while(i <= 10);

Output =
1
2
3
4
5
6
7
8
9
10

OK. So some of you may be thinking what the *, and some may be thinking i remember this. Let's get us all on the same page.

For a start, the above code is correct other that instead of '$i' i have used 'i' so it reads easier. 'echo' simply means write out the line, '/n' means start a new line.

Notice in all the loops 'i' is the index, and we use this to count how many times the loop has gone round. We also need to tell the loop to stop at some point, that is what the '10' is doing, it says once 'i' is equal to 10 stop the loop. The important thing to notice here is that we could change the '10' to '100', '1000' or what ever we like. See how the loop makes life simpler. Another thing to notice in all the loops is the fact that 'i' needs to be incremented, 'i++' increments 'i' by 1, or in lay terms adds 1 to 'i'.

So there we have the basics needed for a loop, a start number, a max number and a way to increment start number.

So what's the difference

Not much between a 'while' loop and a 'for' loop other than syntax. The 'for' loop initialises all its variables and conditions within the 'for' statement, whereas the 'while' loop only states the condition, the variables are initialised outside of the 'while' loop and incremented within.

The 'do while' loop and 'while' loop differ only slightly, the 'while' loop checks the loop conditions before entering the loop, whereas the 'do while' loop checks the loop conditions at the end of the loop. So in the examples above if i say that 'i = 11' then in the 'while' loop nothing is printed as it checked if 'i' was equal too or less than '10', whereas in the 'do while' loop it would print '11' because 'i' is checked after the echo statement.

What's the fourth loop type you mentioned?

Glad your still here and concentrating. The 'foreach' loop is specifically used for arrays, not to say the the others can't be used for arrays.

The 'foreach' loop is like the 'for' loop but all we need to declare is the array to loop and the output variable, see below to understand.


$array = array(1, 2, 3);

foreach( $array AS $number){
 echo "Number: $number /n";
}

output =
Number: 1
Number: 2
Number: 3

If i where to do the above using a 'for' loop it would look like below


array = array(1, 2, 3);
num = count(array);
for( i = 1; i <= num; i++ ){
 echo "Number: i /n";
}

output =
Number: 1
Number: 2
Number: 3

Did you notice the 'num = count(array)', count() is a very valuable PHP function, which counts the number of elements within an array, so above we would get 3.


At the moment that's all I can think of pertaining to loops, if I forgot anything or you think I should add something then please comment or email or send out smoke signals lol.