SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: loop question.

  1. #1
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    loop question.

    I have a range of numbers that are coming into a loop. the problem is that I only want the loop to go however many times this specific variable is set to. Say for instance the variable
    PHP Code:
    $hm '4'
    Now I would want the loop to run 4 times. How do I create that?

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $hm 4;
    $i 0;
    while(
    $i hm)
    {
      
    $i++;


  3. #3
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try using a for loop:



    PHP Code:
    $hm 4

    for ($i 0$i =< $hm$i ++)
    {
    //do code 4 times


    EDIT: grr beat me to it
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  4. #4
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks webnoob exactly what I was looking for. I just couldn't wrap my head around it.

  5. #5
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem. I get the following error
    Code:
    Parse error: syntax error, unexpected '<' in /home/cmuench/public_html/dev/admin/admin.php on line 259
    PHP Code:
    for ($i 0$i =< $hm$i ++)
    {
    // code 

    Also I need to know what number its at inside the loop.

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $hm = 4 line was missing a semicolon and the arguments in for() are separated by semicolons instead of commas. The less than or equal to operator is <= rather than =<, and also that code would run your loop 5 times instead of 4.

    Try it this way
    PHP Code:
    $hm 4;
    for(
    $i 0$i $hm$i++) 

    echo 
    $i '<br>';


  7. #7
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL, oops sorry. WebNoob <----TIRED
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eh, I made it sound worse than it really was

  9. #9
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

  10. #10
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem


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
  •