If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

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.

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 />'
}

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.

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 />'
}

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.

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?

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.

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?