What is difference between for and for each loop.

What is difference between for and for each loop.Please explain with a example.

Your Answer

FOR EACH LOOP work only on arrays.In this the iterations depend on the number of values on the array.But in FOR LOOP we can set the number of iterations by adjusting counter value.
FORLOOP EXAMPLE:
<?php
for( $i = 0 ; $i < 5 ; $i++)
{
$mul= 10 * $i;
echo "result of 10*$i is $mul.<br>";
}
?>
Output:
result of 10*0 is 0.
result of 10*1 is 10.
result of 10*2 is 20.
result of 10*3 is 30.
result of 10*4 is 40.


FOREACH LOOP EXAMPLE:
<?php
$alp = arr("ab"," bc","cd","de");
foreach($alp as $ arr_values)
{
echo $arr_values."<br>"
}

?>
 
Output:
​ab
bc
cd
de

Html

Didn't get the answer.
Contact people of Talent-Html directly by clicking here