|
-
Nov 17th, 2005, 06:52 PM
#1
Thread Starter
Admodistrator
Switch inside a for loop?
Okay, i want to do something on every iteration of my loop(except every 4th), and i want to do something else on every 4th loop.
PHP Code:
for ($i = 0; $i < strlen($stuff);$i++){
switch(bcmod($i,4)){
case '0':
echo 'stuff here';
return;
default:
echo ord(substr($stuff,$i,1)).'.';
return;
}
}
But instead, it will echo 'stuff here' and thats all.. any ideas?
-
Nov 17th, 2005, 07:44 PM
#2
Re: Switch inside a for loop?
you could try
PHP Code:
$max = 10;
for( $i = 1; $i < $max; $i++ ) {
if( $i % 4 == 0 ) {
// do code here for every fourth ;)
} else {
// do your code for all others
}
}
well, that is what i thought you needed and this is untested code. just an idea. hope this helps.
-
Nov 17th, 2005, 07:55 PM
#3
Thread Starter
Admodistrator
Re: Switch inside a for loop?
Yeah that probably works, im just trying to remember how to use Switch, so if i were to use it how would i do it?
PHP Code:
switch($i%){
case '0':
echo 'stuff here';
return;
default:
echo ord(substr($stuff,$i,1)).'.';
return;
}
Parse errors though, how do i do it?
-
Nov 17th, 2005, 08:01 PM
#4
Re: Switch inside a for loop?
 Originally Posted by |2eM!x
Yeah that probably works, im just trying to remember how to use Switch, so if i were to use it how would i do it?
PHP Code:
switch($i%){
case '0':
echo 'stuff here';
return;
default:
echo ord(substr($stuff,$i,1)).'.';
return;
}
Parse errors though, how do i do it?
what are the errors which line?
-
Nov 17th, 2005, 08:03 PM
#5
Re: Switch inside a for loop?
PHP Code:
switch($i){
case 0:
echo 'stuff here';
break;
default:
echo ord(substr($stuff,$i,1)).'.';
break;
}
how about this one?
return i think will get out of your working function so if you want to continue with the others just use break
-
Nov 17th, 2005, 08:07 PM
#6
Thread Starter
Admodistrator
Re: Switch inside a for loop?
well i need to see if it is divisible by another number..so i need the % in there somewhere..i will use break however..messed up the words :S
-
Nov 17th, 2005, 09:32 PM
#7
Re: Switch inside a for loop?
switch ($i % 4) or something
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|