5
Reply

Difference between Foreach and For Loop using php

Bhushan Singh

Bhushan Singh

10y
3k
0
Reply

    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

    For is controlled While foreach is not

    - for each is use for get value is array