0% found this document useful (0 votes)
12 views

PHP Loops

Loops in php

Uploaded by

sahilsahil69221
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

PHP Loops

Loops in php

Uploaded by

sahilsahil69221
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PI-IP Loops

Often when you write code, you want the same block
of code to run over and over again a certain number of
times. So, instead of adding several almost equal
code-lines in a script, we can use loops.

Loops are used to execute the same block of code


again and again, as long as a certain condition is true.

In PHP, we have the following loop types:

while - loops through a block of code as long


as the specified condition is true
do... while - loops through a block of code
once, and then repeats the loop as long as the
specified condition is true
for - loops through a block of code a specified
number of times
foreach - loops through a block of code for
each element in an array
PHP for Loop
PHP for loop is used when you know
exactly how many times you want to
iteratethrough a block of code. It consists
of three expressions:

• Initialization: Sets the initial value of the


loop variable.
• Condition: Checks if the loop should
continue.
• Increment/Decrement:Changes the
loop variable after each iteration.

Syntax

for ( Initialization;
Condition; Increment/ Decrement

// Code to be executed
or oo

Update expression d'

Tru e

Execute loop body

loop
Example: Printing numbers from 1 to 5
using for loop.
PH p

1 <?php
2
3 // Code to
illustrate for loop
4 for ($num 1; $num
5; $num +=
5 echo $num

6
7
8

Output

12345
PHP while Loop
The while loop is also an entry control loop
like for loops. It first checks the condition
at the start of the loop and if its true then it
enters into the loop and executes the block
of statements, and goes on executing it as
long as the condition holds true.

Syntax

while ( condition ) {
// Codeis executed

hile loo
start

Test False
conditio

True

Execute loop body

while loop en
Example: Printing numbers from 1 to 5.

1 <?php
2
3 $num
4
5 while ($num <=
6 echo $num

7 $num++;
8
9

10

Output

12345
PHP do-while Loop
The do-while loop is an exit control loop
which means, it first enters the loop,
executes the statements, and then checks
the condition. Therefore, a statement is
executed at least once using the do...while
loop. After executing once, the program is
executed as long as the condition holds
true.

Syntax

do {
// Codeis executed
} while ( condition ) ;

while loo
tart

Execute loop body

Test False
conditio

True

while loop en
Example: Printing numbers from 1 to 5.

1 <?php
2
3 $num
4
5 do {
6 echo $num

7 $num++;
8 } while ($num <=
5);
9

10

Output

12345
PHP foreach Loop
This foreach loop is used to iterate over
arrays. For every counter of loop, an array
element is assigned and the next counter
is shifted to the next element.

Syntax:

foreach ( $array as $value ) {


// Code to be executed

// or

foreach ( $array as $key =>


$value ) {
// Code to be executed
<?php
$colors - array ("red "green" "blue"
"yellow") •

foreach ($colors as $x) {


$x <br>";

</body>
< / html >

red
green
blue
yellow

You might also like