Interviews6 answers
Difference between Foreach and For Loop using php
6 Answers
foreach loop mostly we use for dynamic array. Because we don’t know about counter. for loop always use when I know counte
Anant SinghPosted Apr 11, 2019, 3:54 AM EDT
For is controlled
While foreach is not
Mukesh KumarPosted Sep 4, 2017, 11:59 PM EDT
- for each is use for get value is array
sok sophaPosted Nov 2, 2015, 6:08 AM EST
sri kanthPosted Nov 17, 2014, 7:23 PM EST
sri kanthPosted Nov 17, 2014, 7:23 PM EST
1. Foreach Loop
a) Foreach loop fully depend array because you have an array , without array it's not worked.
b) foreach Loop working at the end of array.
c) Foreach is great for iterating through arrays that use keys and values.
Eg :Syntax is following.
$arr = array("Gourav","amit","Manoj","sonu","Rana","radhika "); // to define an array
foreach($array as $name ){
echo "Company Employe Name is ".$name.".";
}
Result as following.
Company Employe Name is Gourav.
Company Employe Name is amit.
Company Employe Name is Manoj.
Company Employe Name is sonu.
Company Employe Name is Rana.
Company Employe Name is radhika.
2. For Loop
a) For loop used according to condition.
b) Loop working at the end of given condition.
c) for loop is general purpose counter-based loop
Eg : Syntax is following.
$arr = array("Gourav","amit","Manoj","sonu","Rana","radhika "); // to define an array
for($i=0;$i<6;$i++){
echo " Company Employe Name is ".$arr[$i]."
" ; } Result as following. Company Employe Name is Gourav Company Employe Name is amit Company Employe Name is Manoj Company Employe Name is sonu Company Employe Name is Rana Company Employe Name is radhika
" ; } Result as following. Company Employe Name is Gourav Company Employe Name is amit Company Employe Name is Manoj Company Employe Name is sonu Company Employe Name is Rana Company Employe Name is radhika
Bhushan SinghPosted Oct 15, 2014, 8:35 PM EDT

