The PHP foreach Loop

The foreach loop condition is different that for look,  to execute foreach look , the array condition is must , each array parameters is works as "key" point for for each loop ,below code will explain  much better. 


Syntax : 

foreach ($yourArray as $keyValue) {
   $KeyValue handle code to be executed here;
}


Code :


<!DOCTYPE html>
<html>
<body>

<?php 
$Fruits= array("mango""apple""banana""kiwi"); 

foreach ($Fruits as $keyValue) {
    echo "$KeyValue <br>";
}
?>

</body>
</html>


Output :

mango
apple
banana
kiwi



Scenario : If condition under foreach loop. 

<!DOCTYPE html>
<html>
<body>

<?php 
$Fruits= array("mango""apple""banana""kiwi"); 

foreach ($Fruits as $keyValue) {
    
   if($keyValue=="banana")
   {
     echo "I like banana"
   }

}
?>


</body>
</html>


Output :
"I like banana" 

No comments :