Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation Can a for loop be put inside an If Statement?

    Hi, the title is a little misleading but basically what I'd like to know is whether it's possible to execute loop conditional on an if statement:

    <?php
    $num=$_GET['id'];
    if($num>5){
    for($counter=0;$counter>5;$counter++){
    }else{
    for($counter=5;$counter<0;$counter--){
    }
    echo $counter . "<br />";
    }
    ?>

    Obviously that code won't work, because it runs into a problem with the loop not being properly closed. What I want to do is have a different for statement pending a certain condition is met.

    My actual code is much much much longer, hence why I don't want to write two for loops - so is there a way to do this? I hope I explained myself properly.

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Yes.
    You can put for loops inside conditional.

  • #3
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Thumbs down The Cheese Stands Alone - Or Why My Previous Plea for Help Failed.

    ok maybe I need to explain it better, basically I'm retrieving a command from the user.

    If they give me command 1 - I want to have a for loop that iterates 4 times forward, so it counts from 1-4 (1,2,3,4).

    If they give command 2 - I want to have a for loop that iterates 4 times backwards, so it counts from 4-1 (4,3,2,1).

    I want to use an if statement, to output the first part of the for loop within the if statement itself, I don't know if this is possible - I have a lot of stuff within the for loop, so I'm trying to do it all in one for loop otherwise I'd have 2 long for loops - which I'm trying to avoid and stop from getting too much cluttered code.

    The loop will not contain the entire for loop, only the first part which states:
    for ($i=1;$i<5;$i++){

    so
    Code:
    if(statement){
    for ($i=1;$i<5;$i++){
    }else{
    for ($i=5;$i<1;$i--){
    }
    //notice the rest of the loop is outside the if structure
    echo $i . '<br />'
    }
    Last edited by BatCountry; 08-20-2008 at 12:07 AM.

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Why not have it inside the if and for structure? I don't see a problem with that?

  • #5
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts
    because then I'd have to two seperate for loops, and as I mentioned above - the code inside the for loops is very very very very long, so it would be redundant to have to insert what's basically the same code twice. I'm trying to just output the first part of the for loop that way I can just have the giant block of code once, rather then twice.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by BatCountry View Post
    ok maybe I need to explain it better, basically I'm retrieving a command from the user.

    If they give me command 1 - I want to have a for loop that iterates 4 times forward, so it counts from 1-4 (1,2,3,4).

    If they give command 2 - I want to have a for loop that iterates 4 times backwards, so it counts from 4-1 (4,3,2,1).

    I want to use an if statement, to output the first part of the for loop within the if statement itself, I don't know if this is possible - I have a lot of stuff within the for loop, so I'm trying to do it all in one for loop otherwise I'd have 2 long for loops - which I'm trying to avoid and stop from getting too much cluttered code.

    The loop will not contain the entire for loop, only the first part which states:
    for ($i=1;$i<5;$i++){

    so
    Code:
    if(statement){
    for ($i=1;$i<5;$i++){
    }else{
    for ($i=5;$i<1;$i--){
    }
    //notice the rest of the loop is outside the if structure
    echo $i . '<br />'
    }
    this don't work but since is and example doesn't matter,
    PHP Code:
    for ($i=5;$i<1;$i--){ 
    you could do something like this:
    PHP Code:
    for(statement $i=$i=5;
         
    statement $i<$i <1;
         
    statement $i++ : $i--){
      
    //notice the rest of the loop is outside the if structure
      
    echo $i '<br />'

    but I suggest to avoid this. A better solution is to put your very very very ... long code in functions and call them in two for loops.

    regards
    Last edited by oesxyl; 08-20-2008 at 12:37 AM.

  • #7
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts
    putting it into a function doesn't quite work because there's a lot of variables that have to be passed to the function -- too many of which would have to be declared global or passed to it back and forth.

  • #8
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I tried putting the code into a file and including it within the loop

    Code:
    if(a=1){
        for($q=0;$q<5;$q++){
         include('somefunctions.php');
        }
    }else{
        for($q=5;$q>0;$q--){    
         include('somefunctions.php');
        }
    }
    But it didn't work, the include php file was not processed but just included as text. For example, I had statements in somefunctions.php like this echo $q; (it's using the loop's $q variable - and thus I think it wouldn't recognize $q as the server processes php files before it includes them?

  • #9
    New Coder
    Join Date
    Jul 2008
    Posts
    91
    Thanks
    4
    Thanked 9 Times in 9 Posts
    ahh... i just noticed this (after the 50th read of your code):

    PHP Code:
    if(a=1){ 
    that should say:
    PHP Code:
    if($a=1){ 
    Current Project: Nothing at the minute

  • #10
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,301
    Thanks
    4
    Thanked 203 Times in 200 Posts
    If you're trying to test whether or not the variable $a is equal to 1 you'd best use
    PHP Code:
    if($a==1){ 
    In your example scoop_987 the value of 1 will be assigned to $a.
    Dave .... HostMonster for all of your hosting needs

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by BatCountry View Post
    putting it into a function doesn't quite work because there's a lot of variables that have to be passed to the function -- too many of which would have to be declared global or passed to it back and forth.
    using such a twisted way to do things is a sign that you must redesign and reimplement all that stuff.

    Quote Originally Posted by BatCountry View Post
    I tried putting the code into a file and including it within the loop

    Code:
    if(a=1){
        for($q=0;$q<5;$q++){
         include('somefunctions.php');
        }
    }else{
        for($q=5;$q>0;$q--){    
         include('somefunctions.php');
        }
    }
    But it didn't work, the include php file was not processed but just included as text. For example, I had statements in somefunctions.php like this echo $q; (it's using the loop's $q variable - and thus I think it wouldn't recognize $q as the server processes php files before it includes them?
    it's something wrong with somefunctions.php, $q from this code could be used in included file. Can you post that file?

    best regards


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •