Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

    While Loop hiccup?

    I'm sure it's simple, but what am I missing?
    When I run this:
    PHP Code:
    $test=array('to','the','moon','alice');

    if(
    is_array($test)){
        
    $rowCount=count($test);
        
    $i=0;
        while(
    $i<=$rowCount){
            
    $var=$test[$i];
            switch(
    $var){
                case 
    "to":
                    
    $blah="To";
                    break;
                case 
    "the":
                    
    $blah=" the";
                    break;
                case 
    " moon":
                    
    $blah="moon";
                    break;
                case 
    "alice":
                    
    $blah=" Alice!";
                    break;
            }
            print 
    $blah;
            
    $i++;

    I am getting "To the moon Alice! Alice!".
    Why am I getting a repeat on my last value?

    ~ Mo

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Code:
    while ($i <= $rowCount)
    to
    Code:
    while ($i < $rowCount)

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Aaahh!
    Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •