Results 1 to 7 of 7

Thread: PHP while Loop

  1. #1
    Join Date
    Apr 2005
    Posts
    46,704

    Default PHP while Loop

    The while loop executes a block of code while a condition is true.


    Code:
    <html>
    <body>
    
    <?php
    $i=1;
    While($i<=5)
    {
    echo "The number is" . $i . "<br />";
    $i++;
    }
    
    ?>
    
    </body>
    </html>

    Out Put:

    The number is1
    The number is2
    The number is3
    The number is4
    The number is5




    Loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
    Last edited by minisoji; 10-24-2011 at 07:07 AM.

  2. #2
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    do...while Statement

    Code:
    <html>
    <body>
    
    <?php
    $i=1;
    do
    {
    
    $i++;
    echo "The number is" . $i . "<br/>";
    }
    while($i<=5)
    
    
    ?>
    
    </body>
    </html>

    Out Put:

    The number is2
    The number is3
    The number is4
    The number is5
    The number is6



    Starts with i=1. It will then increment i with 1, and write some output. Then the condition is checked, and the loop will continue to run as long as i is less than, or equal to 5:

  3. #3
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    The for Loop

    Code:
    <html>
    <body>
    
    <?php
    for ($i=1; $i<=5; $i++)
      {
      echo "The number is " . $i . "<br />";
      }
    ?>
    
    </body>
    </html>

    Out Put:

    The number is 1
    The number is 2
    The number is 3
    The number is 4
    The number is 5


    Loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs

  4. #4
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    The foreach Loop

    Code:
    <html>
    <body>
    
    <?php
    $x = array("one", "two", "three");
    foreach ($x as $value)
    {
    
    echo $value . "<br/>";
    
    }
    ?>
    
    </body>
    </html>
    Out Put:

    one
    two
    three


    It will print the values of the given array

  5. #5
    Join Date
    Nov 2011
    Posts
    4

    Default

    very nice to have this. this is the same like as the C++. if you know C++ then you attempt it by yourself. is this?
    other wise you have to learn this by default.

  6. #6
    Join Date
    Mar 2012
    Posts
    6

    Default

    Thankyou ^_^

  7. #7
    Join Date
    Apr 2012
    Posts
    6

    Default

    it's not hard to study

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
  •