Results 1 to 7 of 7

Thread: Switch inside a for loop?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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?

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Switch inside a for loop?

    you could try

    PHP Code:

    $max 
    10;
    for( 
    $i 1$i $max$i++ ) {
         if( 
    $i == ) {
              
    // 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.

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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?

  4. #4
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    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?

  5. #5
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    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

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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

  7. #7
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    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
  •  



Click Here to Expand Forum to Full Width