For and foreach Loop In PHP | PHP Lecture 8 | How to do this

Опубликовано: 27 Февраль 2026
на канале: JusticeLens Cam
392
13

What is For loop, What is Foreach Loop, What is repetition or Loop Control statement.
#ForLoopInPHP #ForeachLoopInPHP #HowToDoThis


For more Queries
Join us on facebook:|

  / 1184520091737540  


PHP For Loop:

PHP for loop can be used to traverse set of code for the specified number of times.

It should be used if the number of iterations is known otherwise use while loop. This means for loop is used when you already know how many times you want to execute a block of code.

It allows users to put all the loop related statements in one place.
See in the syntax given below:

Syntax:
for(initialization; condition; increment/decrement){
//code to be executed
}



Parameters

The php for loop is similar to the java/C/C++ for loop. The parameters of for loop have the following meanings:

initialization - Initialize the loop counter value. The initial value of the for loop is done only once. This parameter is optional.

condition - Evaluate each iteration value. The loop continuously executes until the condition is false. If TRUE, the loop execution continues, otherwise the execution of the loop ends.

Increment/decrement - It increments or decrements the value of the variable.


PHP foreach Loop:
The foreach loop - Loops through a block of code for each element in an array.
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.


Syntax:
foreach ($array as $value) {
code to be executed;
}

For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.