Results 1 to 6 of 6

Thread: How to use For-Each Loop in PHP?

  1. #1
    Join Date
    Aug 2006
    Posts
    222

    How to use For-Each Loop in PHP?

    Hello friends,
    I have just started the PHP programming language, so you can consider me as a rookie. I have done with some loop in PHP but I get confused when the for-each loop comes.!! I thought that you guys can explain me in better way. So please tell me how to use For-Each Loop in PHP? Hope that you would help me as soon as possible.!!
    Just a reply to say thank you for these links and posts I have a lot to read and learn now!



  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to use For-Each Loop in PHP?

    The loops are the most complex loops in PHP. They work like loops of the C language syntax for loops is as follows :
    PHP Code:
    for (expr1expr2expr3)
        
    statement 
    The first expression (expr1) is evaluated (executed), what happens at the beginning of the loop. At the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the statement is executed. If it evaluates to FALSE, the execution of the loop ends. At the end of each iteration, expr3 is evaluated (executed).

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to use For-Each Loop in PHP?

    I would like you to tell about the For Loop, since it is an important and complex loop. Expressions can be empty or may contain multiple expressions separated by commas. Expr2 In all expressions separated by a comma are evaluated but the result is obtained since the last part. If expr2 is left blank, meaning it is an infinite loop. PHP implicitly considers it as TRUE, like C. This is not really very useful, unless you want to end the loop by the conditional break.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to use For-Each Loop in PHP?

    PHP 4 introduced a foreach construct, much like Perl or other languages. It is a simple way to browse a table. Foreach works only on arrays, and return an error if you try to use a variable of another type or uninitialized. The following are two syntaxes of for-each loop :
    PHP Code:
    foreach (array_expression as $value)
        
    statement
    foreach (array_expression as $key => $value)
        
    statement 
    The first form loops over the array array_expression. At each iteration, the value of the current element is assigned to $ value and the internal array pointer is advanced by one. The second form is exactly the same thing, but the key to the current element is assigned to the variable $ key.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to use For-Each Loop in PHP?

    There are some things that you should know before using the for-each loop into the coding. When foreach first starts, the internal array pointer is automatically reset to the first array element. This means you will not have to call reset () before a foreach loop. Unless the array is a reference, foreach operates on a copy of the specified array and not the array itself. Foreach affects the internal array pointer. Do not use without resetting before.

  6. #6
    Join Date
    Nov 2008
    Posts
    996

    Re: How to use For-Each Loop in PHP?

    Since PHP 5, you can easily modify elements of an array by preceding $ value of &. This will assign a reference instead of copying the value. The following example may help you in understanding the topic :
    PHP Code:
    <?php
    $arr 
    = array(12345);
    foreach (
    $arr as &$value) {
        
    $value $value 2;
    }

    unset(
    $value);
    ?>
    Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

Similar Threads

  1. Watercooling: Single loop or Dual Loop
    By Akolekar in forum Hardware Peripherals
    Replies: 3
    Last Post: 21-10-2011, 10:52 PM
  2. Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work
    By mADiRAkSHii in forum Operating Systems
    Replies: 4
    Last Post: 25-01-2011, 07:23 PM
  3. How to use Do-While Loop in PHP?
    By Zavier in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 12:34 AM
  4. Which is best for iterator: For loop or while loop
    By Leeland in forum Software Development
    Replies: 4
    Last Post: 18-01-2010, 06:03 PM
  5. Differentiate between Do-While loop and While loop
    By REDBULL in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 10:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,307,505.69061 seconds with 17 queries