Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to use Do-While Loop in PHP?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 05-03-2010
Member
 
Join Date: Jun 2009
Posts: 347
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.!!

Reply With Quote
  #2  
Old 05-03-2010
Member
 
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....
Reply With Quote
  #3  
Old 05-03-2010
Member
 
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
Reply With Quote
  #4  
Old 05-03-2010
Member
 
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.
Reply With Quote
  #5  
Old 05-03-2010
Member
 
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.
Reply With Quote
  #6  
Old 05-03-2010
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use Do-While Loop in PHP?"
Thread Thread Starter Forum Replies Last Post
Watercooling: Single loop or Dual Loop Akolekar Hardware Peripherals 3 21-10-2011 10:52 PM
Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work mADiRAkSHii Operating Systems 4 25-01-2011 06:23 PM
How to use For-Each Loop in PHP? Bigga Lexx Software Development 5 06-03-2010 12:16 AM
Which is best for iterator: For loop or while loop Leeland Software Development 4 18-01-2010 05:03 PM
Differentiate between Do-While loop and While loop REDBULL Software Development 3 26-11-2009 09:10 AM


All times are GMT +5.5. The time now is 08:52 PM.