PHP Loop: Soren Technical Institute IT Department
PHP Loop: Soren Technical Institute IT Department
IT department
PHP Loop
Suppose you want to echo a statement 20 times and you don’t know
about PHP loops. You start typing the same statement 20 times. This
takes time and more human effort. Now, to save this extra effort, you can
use Loops with the condition that perform the repetitive task for 20 times.
Types of PHP loop
There are four types of loops in PHP. You can
use these tasks to perform repetitive tasks:-
● PHP while:- While condition executes a block of code
again and again until the specified condition is true.
● PHP do…while:- It first executes the block of code and
then after executes this block of code again and again
until the specified condition is true.
● PHP for:- For condition executes a block of code at a
specified numb of times.
● PHP foreach:- The loop can be used to iterate through
each element of an array.
PHP WHILE LOOP
PHP while loop executes a block of code again and
again until the given condition is true. The loop first test
the condition if it is true and if the condition is true, it
executes the code and returns back to check the
condition if it is true.
The process repeats itself and executes the code. If the
condition is false, the loop stops executing the code
and stop the loop also.
EXAMPLE OF PHP WHILE LOOP
THE EXAMPLE PRINTS THE NUMBER FROM 0 TO 9.
OUTPUT
PHP DO…WHILE LOOP
PHP do…while loop first executes a block of code and
then after checks the given condition is true. The code
will execute for the second time only if the given
condition is true and the loop repeats itself again and
again until the condition is true.
Example of PHP DO…..While loop
the example prints the number from 0 to 9.
OUTPUT
OUTPUT
PHP FOR LOOP