PHP For Each

PHP has foreach function that helps to perform tasks in loop. We have for loop in PHP using which we can also perform task in loop. Foreach function is alternate to for loop.

PHP For Each Loop

Lets print all the values present in an array. 

Example

<?php
$fruitNames=array("apple","banana","orange","guava","jackfruit");
foreach($fruitNames as $fname){
echo $fname;
}
?>
output
apple
banana
orange
guava
jackfruit

Lets print 1 to 10 using foreach loop in PHP

<?php
foreach (range(1,10) as $i) {
echo $i;
}
?>

Output
1
2
3
4
5
6
7
8
9
10 
PHP For Each PHP For Each Reviewed by Pixel Stream on December 10, 2019 Rating: 5

No comments:

Powered by Blogger.