How to use Loops in PHP - Web Development
110 Views

How to use Loops in PHP

There are three types of loop in php : While loop , do while loop , for & for-each loop

While Loop :- loops through a block of code unitl the specified condition become true

Now below is first example of while loop

Output of code would be this :

4
8
12
16
20
24
28
32
36
40
44
48
52
56
60

Now i will teach you how to write for loop code . The for loop is used when you know in advance how many times the your script will run

for (init counter; test counter; increment counter)

{
  Your code logic ;
}

Output of codw would be this

No is: 5 
No is: 6 
No is: 7 
No is: 8 
No is: 9 
No is: 10 
No is: 11 
No is: 12 
No is: 13 
No is: 14 
No is: 15 
No is: 16 
No is: 17 
No is: 18 
No is: 19 
No is: 20 

The loop output starts from 5 and end to 20 becuase x = 5 and till x = < 20

Now we will see for-each loop . This loop is only for array when you use array that time for- each loop can be used

foreach ($array as $value)

{
Your code logic;
}

Now we will see one example in for – each loop

Output of code would be this

Check these tutorials in php

Leave a Reply

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