1. I've started a massive SPAM purge, any accounts that have spammed are being removed, also for a short period new user signups are disallowed until the SPAM cleaning is finished.

what is the difference between for and foreach?

Discussion in 'PHP' started by shepherd, Mar 27, 2013.

  1. shepherd

    shepherd Member

    Joined:
    Feb 28, 2013
    Messages:
    95
    Likes Received:
    0
    Trophy Points:
    6
    Gender:
    Male
    Loading...
  2. shepherd

    shepherd Member

    Joined:
    Feb 28, 2013
    Messages:
    95
    Likes Received:
    0
    Trophy Points:
    6
    Gender:
    Male
    for is expressed as follows:
    for (expr1; expr2; expr3)
    statement
    The first expression is executed once at the beginning. In each iteration, expr2 is evaluated. If it is TRUE, the loop continues and the statements inside for are executed. If it evaluates to FALSE, the execution of the loop ends. expr3 is tested at the end of each iteration.

    thumbtack clone | taskrabbit clone | agent anything clone
  3. mtrick

    mtrick Member

    Joined:
    Nov 17, 2011
    Messages:
    164
    Likes Received:
    4
    Trophy Points:
    18
    Gender:
    Male
    The first expression is executed once at the beginning. In each iteration, expr2 is evaluated. If it is TRUE, the loop continues and the statements inside for are executed
  4. semaphore.v

    semaphore.v Member

    Joined:
    Feb 19, 2013
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    16
    Gender:
    Male
    Foreach loop used when you have an array, without array it's not worked.Loop working at the end of array count. For loop used according to condition.Loop working at the end of given condition.
  5. davidsmith

    davidsmith Member

    Joined:
    Jun 21, 2013
    Messages:
    28
    Likes Received:
    1
    Trophy Points:
    6
    1. Foreach Loop:- Details are following.
    a) Foreach loop used when you have an array, without array it's not worked.
    b) Loop working at the end of array count. For example an array have 5 value then loop run 5 times.

    2. For Loop:- Details are following.
    a) For loop used according to condition.
    b) Loop working at the end of given condition.
  6. msankar

    msankar New Member

    Joined:
    Jun 22, 2013
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Location:
    India
    yes i agree. foreach loop is advanced compare to for loop.
  7. Maureen P. James

    Maureen P. James Member

    Joined:
    Jul 8, 2014
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    6
    Gender:
    Male
    Location:
    UK, london
    For loop is all about performing this operation n times. And foreach loop say's perform this operation against each value/object in this IEnumerable.
  8. Sandeep Hatria

    Sandeep Hatria Member

    Joined:
    Sep 15, 2014
    Messages:
    15
    Likes Received:
    26
    Trophy Points:
    13
    Gender:
    Male
    Location:
    Gurgaon
    Hi,
    Differences between foreach and for loop are given in below--

    Foreach loop:

    1.it only works for array means displaying the array contents.
    2.Loop working at the end of the array count.

    for example-
    <?php
    $arr=array("Ram","Shita","Laxman");
    foreach($arr as $name){
    echo "The name of your friend is : ",$name,"<br>";
    }
    ?>


    For loop:

    1.for loop is used according to condition.
    2.loop working at the end of the given condition.

    for example-
    <?php
    $array = array("Surinder","Rahul","Manoj");
    For($i=0;$i<3;$i++)
    {
    echo "Employe Name is ".$array[$i].";
    }

    ?>
  9. anirban09P

    anirban09P Member

    Joined:
    Jun 30, 2014
    Messages:
    196
    Likes Received:
    0
    Trophy Points:
    16
    Gender:
    Female
    what is difference between foreach and for?
    Which gives better perfomance?
    please give reply ........
  10. andysmith

    andysmith Member

    Joined:
    Jun 30, 2014
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    6
    Gender:
    Male
    for isn't generally "n. times", but instead "while this condition is met", which is regularly a basic counter check. Also, you don't really require IEnumerable for foreach, however in reality: it is semantically a "get every thing thus" API.

    infopath signature

Share This Page