Results 1 to 6 of 6

Thread: How to use For Loop in PHP?

  1. #1
    Join Date
    Jul 2006
    Posts
    128

    How to use For Loop in PHP?

    Hello Friends,
    I am newbie in PHP. I have done C and C++ programming language little bit. I want to use the For Loop in PHP but I don't know how to use.! Do we have to set a counter variable to some initial value for doing the For Loop in PHP.?? Does anyone know how to use For Loop in PHP? Please help me to sort out my doubt.!
    "Every man is guilty of all the good he did not do". - Voltaire

  2. #2
    Join Date
    Aug 2006
    Posts
    235

    Re: How to use For Loop in PHP?

    Since you have done C and C++ programming language, you should know why we use the For Loop in PHP. Because the concept are same. Also you have to set a counter variable to some initial value for doing the For Loop in PHP. Here are some common tasks that are covered by a for loop are :
    • Set a counter variable to some initial value.
    • Execute the code within the loop.
    • Check to see if the conditional statement is true.
    • Increment a counter at the end of each iteration through the loop.

    The for loop allows you to define these steps in one easy line of code.
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  3. #3
    Join Date
    Nov 2008
    Posts
    996

    Re: How to use For Loop in PHP?

    If you know how many times the script should run before writing the code, then you can use the for loop. The Syntax of For Loop :
    PHP Code:
    for (initconditionincrement)
      {
      
    code to be executed;
      } 
    The For Loop takes the following parameters :
    • condition : this parameter evaluates for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
    • init : This is mostly used to set a counter.
    • increment : This is used to increment a counter.

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to use For Loop in PHP?

    By looking at the following example I think that your doubt will be solved.!! In the example below the loop that is defined, starts with i=1. The loop will continue to run as long as i is less than, or equal to 7. i will increase by 1 each time the loop runs :
    PHP Code:
    <html>
    <body>

    <?php
    for ($i=1$i<=7$i++)
      {
      echo 
    "The number is " $i "<br />";
      }
    ?>

    </body>
    </html>
    The output of the above coding will be :
    Code:
    The number is 1
    The number is 2
    The number is 3
    The number is 4
    The number is 5
    The number is 6
    The number is 7

  5. #5
    Join Date
    Mar 2008
    Posts
    349

    Re: How to use For Loop in PHP?

    The basic structure of the for loop is as follows :
    PHP Code:
    for ( initialize a counterconditional statementincrement a counter){
        do 
    this code;

    You should note that each step is separated by a semicolon: initiliaze counter, conditional statement, and the counter increment. Since the semicolon are separate expressions, so they are needed. Also note that a semicolon is not needed after the "increment counter" expression. These are some common mistakes made by the beginners. So avoid making such mistakes.

  6. #6
    Join Date
    Mar 2008
    Posts
    672

    Re: How to use For Loop in PHP?

    You can also use the foreach loop in PHP. The foreach loop is used to loop through arrays. The below coding is an example of the foreach loop :
    PHP Code:
    <html>
    <body>

    <?php
    $x
    =array("red","green","blue");
    foreach (
    $x as $value)
      {
      echo 
    $value "<br />";
      }
    ?>

    </body>
    </html>
    The output of the above code will be :
    Code:
    red
    blue
    green

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,714,697,140.77172 seconds with 17 queries