Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-19-2008, 10:13 PM   PM User | #1
BatCountry
New Coder

 
BatCountry's Avatar
 
Join Date: Jul 2007
Location: McMaynerberry, Texas
Posts: 57
Thanks: 5
Thanked 0 Times in 0 Posts
BatCountry can only hope to improve
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.
BatCountry is offline   Reply With Quote
Old 08-20-2008, 12:04 AM   PM User | #2
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
Yes.
You can put for loops inside conditional.
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 08-20-2008, 12:05 AM   PM User | #3
BatCountry
New Coder

 
BatCountry's Avatar
 
Join Date: Jul 2007
Location: McMaynerberry, Texas
Posts: 57
Thanks: 5
Thanked 0 Times in 0 Posts
BatCountry can only hope to improve
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..
BatCountry is offline   Reply With Quote
Old 08-20-2008, 12:09 AM   PM User | #4
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
Why not have it inside the if and for structure? I don't see a problem with that?
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 08-20-2008, 12:26 AM   PM User | #5
BatCountry
New Coder

 
BatCountry's Avatar
 
Join Date: Jul 2007
Location: McMaynerberry, Texas
Posts: 57
Thanks: 5
Thanked 0 Times in 0 Posts
BatCountry can only hope to improve
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.
BatCountry is offline   Reply With Quote
Old 08-20-2008, 12:33 AM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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..
oesxyl is offline   Reply With Quote
Old 08-20-2008, 01:36 AM   PM User | #7
BatCountry
New Coder

 
BatCountry's Avatar
 
Join Date: Jul 2007
Location: McMaynerberry, Texas
Posts: 57
Thanks: 5
Thanked 0 Times in 0 Posts
BatCountry can only hope to improve
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.
BatCountry is offline   Reply With Quote
Old 08-20-2008, 07:17 PM   PM User | #8
BatCountry
New Coder

 
BatCountry's Avatar
 
Join Date: Jul 2007
Location: McMaynerberry, Texas
Posts: 57
Thanks: 5
Thanked 0 Times in 0 Posts
BatCountry can only hope to improve
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?
BatCountry is offline   Reply With Quote
Old 08-20-2008, 07:40 PM   PM User | #9
scoop_987
New Coder

 
Join Date: Jul 2008
Posts: 91
Thanks: 4
Thanked 9 Times in 9 Posts
scoop_987 is an unknown quantity at this point
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
scoop_987 is offline   Reply With Quote
Old 08-20-2008, 08:24 PM   PM User | #10
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
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
djm0219 is offline   Reply With Quote
Old 08-21-2008, 02:27 AM   PM User | #11
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Tags
code, error, message, php, problem

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:13 PM.


Advertisement
Log in to turn off these ads.