Bhushan Singh
Difference between Foreach and For Loop using php
By Bhushan Singh in PHP on Oct 16 2014
  • Bhushan Singh
    Oct, 2014 16

    1. Foreach Loopa) 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 Loopa) For loop used according to condition. b) Loop working at the end of given condition. c) for loop is general purpose counter-based loopEg : 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

    • 2
  • Anant Singh
    Apr, 2019 11

    foreach loop mostly we use for dynamic array. Because we don’t know about counter. for loop always use when I know counte

    • 0
  • Mukesh Kumar
    Sep, 2017 5

    For is controlled While foreach is not

    • 0
  • sok sopha
    Nov, 2015 2

    - for each is use for get value is array

    • 0
  • sri kanth
    Nov, 2014 18
  • sri kanth
    Nov, 2014 18


Most Popular Job Functions


MOST LIKED QUESTIONS