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?
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.
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?
Re: Switch inside a for loop?
Quote:
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?
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 ;)
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
Re: Switch inside a for loop?
switch ($i % 4) or something :)