eHow launches Android app: Get the best of eHow on the go.

How To

How to Write a While Loop in PHP

Member
By Mike
User-Submitted Article
(0 Ratings)

A "while" loop is simple loop that lets you check a condition at the beginning of the loop before running through the loop's contents.

Here is how you write a "while" loop in PHP.

Difficulty: Easy
Instructions
  1. Step 1

    The "while" statement starts with the word "while" followed by the condition you are checking set in parenthesis, then an opening curly brace indicating the code to follow is within the loop. So the code looks like this:
    While (condition) {

  2. Step 2

    After the opening line it's time to type in your code. This represents that you want to keep executing the code so as long as the condition in the first line is true. So your code could look like this:
    $i=0;
    while ($i<10) {
    $i++;

    The code above uses a variable called $i. As long as $i is less than 10 the code $i++ will continue to execute. That code adds 1 to the value of $i.

  3. Step 3

    The last line of code is a closing curly brace to indicate that code after that brace is not included within the loop. So the final code looks like this:
    $i=0;
    while ($i<10) {
    $i++;
    }

    Note: Somewhere in your loop your condition must eventually evaluate to flase otherwise you will end up in an infinite loop.

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics