For and For Each Loops


For
අපි ගොඩක් වෙලාවට for loop යොදාගන්නේ අපේ script එක run කරන්න ඕනේ අවස්තාගාන හරියටම දන්න වෙලාවල් වලට. Loop එකේ syntax එක එන්නේ මේ විදියට.

for (init;condition;increment){
      ඔයාගේ Code එක 
}

Example

<?php
for($i=1;$i<=5;$i++){
echo "The Number is $i <br/>";
}

?>

Output:

The Number is 1
The Number is 2
The Number is 3
The Number is 4
The Number is 5

For Each 
For Each හැමතිස්සෙම භාවිතා වෙන්නේ arrays එක්ක.

For each ($array us $value){
      ඔයාගේ Code එක 
}

Example

<?php
$s=array("one","two","three");
foreach ($s as $value){
echo "$value <br/>";
}

?>

Output:
one
two
three