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 />'
}
Last edited by BatCountry; 08-20-2008 at 12:07 AM..
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 />'
}
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=1 : $i=5; statement ? $i<5 : $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.
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.
Quote:
Originally Posted by BatCountry
I tried putting the code into a file and including it within the loop
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?