The for loop carry out a block of code a specified number of times the script should run.
Syntax:-
for (init counter; counter compression; increment counter) {
Execution code….;
}
Factors:-
init counter: Initialize the loop counter value
counter compression: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value
Example:-
<?php
for ($x = 0; $x <= 10; $x++) {
for ($y=0; $y <= 10; $y++){
echo $x;
}
}
?>
Foreach Loop:-
The foreach loop works with arrays and used key/value pair in an array.
Syntax:-
foreach ($array as $value) {
execution code…;
}
Example:-
<?php
$clr = array(“red”, “white”, “blue”, “yellow”);

foreach ($clr as $value) {
echo “$value <br>”;
}
?>

Categories: PHP

0 thoughts on “PHP For Loops”

Leave a Reply

Latest Article

Blog

PHP Magic Methods

__get()/__set() – These are magic getters and setters for getting and putting values for class properties created dynamically. __isset() – This magic method will be invoked automatically while checking whether a required overloaded property is Read more…

Blog

WordPress Upgrade

The WordPress upgrade released time to time. We provide a WordPress upgrade, content management and maintenance services at affordable price. WordPress upgrade fixes bugs, runs cleaner, faster, and is more secure and stable. We will Read more…

Blog

Magento 2 Upload image in product

If you are trying to upload image on respective product by script then below code will help you to solve this issue. by this code you can set product thumbnail, small and large image. $objectManager Read more…