5
Reply

Difference between Foreach and For Loop using php

Bhushan Singh

Bhushan Singh

Oct 16, 2014
2.9k
0

    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

    Bhushan Singh
    October 16, 2014
    2

    For is controlled While foreach is not

    Mukesh Kumar
    September 05, 2017
    0

    - for each is use for get value is array

    sok sopha
    November 02, 2015
    0
    sri kanth
    November 18, 2014
    0
    sri kanth
    November 18, 2014
    0