Results 1 to 6 of 6

Thread: How to use Do-While Loop in PHP?

  1. #1
    Join Date
    Jun 2009
    Posts
    361

    How to use Do-While Loop in PHP?

    Hello friends,
    I am new to the PHP programming language. I want to know about the do-while loop that is used in PHP. I have used that loop before but in other programming language. There should be some different way to use it in PHP. So thought to ask you guys, since you explain the topic more clearly. Please tell me how to use Do-While Loop in PHP? I am not so good in PHP, so please explain me in simple language.!!

  2. #2
    Join Date
    Jul 2006
    Posts
    286

    Re: How to use Do-While Loop in PHP?

    You can run the piece of code again and again by using the while loop in PHP. I would like to explain you about the while loop so that you can understand the basic concept very clearly. The while loop is the easiest way to implement a loop in PHP. This loop behaves the same way as in C. The simplest example of a while loop is as follows :
    PHP Code:
    while (expr)
        
    statement 
    IF you hate me, please don't mention it. I know who hates me and who doesn't. You really do not have to make fun of people....

  3. #3
    Join Date
    Jul 2006
    Posts
    289

    Re: How to use Do-While Loop in PHP?

    Even I am explaining you more about the while loop because once you know about the while loop, you can easily understand the do-while loop. The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration. You should know that each time PHP runs the statements in the loop is one iteration. Sometimes, if the while expression evaluates to FALSE from the very beginning, the nested statement(s) won't even be run once.
    Signatures reduce available bandwidth

  4. #4
    Join Date
    Aug 2006
    Posts
    227

    Re: How to use Do-While Loop in PHP?

    As with the if you can group multiple statements within the same while loop by grouping them within parentheses or by using the following syntax :
    PHP Code:
    while (expr):
        
    statement
        
    ...
    endwhile; 
    PHP executes the statement as an expression of the while loop is evaluated as TRUE. The value of the expression is checked at the beginning of each loop, and if the value changes during the execution of the statement, execution will not stop until the end of the iteration
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  5. #5
    Join Date
    Jul 2006
    Posts
    442

    Re: How to use Do-While Loop in PHP?

    The do-while loops are very similar to while loops, but the expression is checked at the end of each iteration rather than the beginning. The main difference from regular while loops is that the first iteration of the do-while loop is always executed where the expression is checked at the end of the iteration, which is not the case when you use a while loop. The condition is checked at the beginning of each iteration, and if it is FALSE from the very beginning, the loop will be stopped immediately.
    "When they give you ruled paper, write the other way..." J.R.J.

  6. #6
    Join Date
    Mar 2008
    Posts
    349

    Re: How to use Do-While Loop in PHP?

    The following is the syntax for do-while loops :
    PHP Code:
    <?php
    $i 
    0;
    do {
        echo 
    $i;
    } while (
    $i 0);
    ?>
    The above loop would run only once, because when the expression is evaluated, it is FALSE (because the variable $ i is not greater than 0) and the execution of the loop 's stops.

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, 06:23 PM
  3. How to use For-Each Loop in PHP?
    By Bigga Lexx in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 12:16 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, 05:03 PM
  5. Differentiate between Do-While loop and While loop
    By REDBULL in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •