How is this array count working?

Discussion in 'PHP' started by gobbly2100, Nov 19, 2008.

  1. #1
    Hey all,

    I am just playing with some basic PHP and I don't understand how this is counting my array, here is the code:

    
    <?php
    
    $names = array("Gobbly", "Loz", "Rayman");
    $namesCount = count($names);
    
    print ($namesCount);
    
    
    ?>
    Code (markup):
    It returns 233 but I don't understand where it is getting that number from, I just wanted to count the amount of elements in my array.

    Any help would be much appreciated, thank you!
     
    gobbly2100, Nov 19, 2008 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    Use This:

    
    $namesCount = 0;
    $names = array("Gobbly", "Loz", "Rayman");
    $namesCount = count($names);
    
    echo $namesCount;
    
    
    Code (markup):
    Chances are its in a loop or namesCount is somehow edited. I unit tested the above code and it gives 3.

    Peace,
     
    Barti1987, Nov 19, 2008 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    You put that script inside a loop I guess? Nothing wrong with the script. It should gives 3.
     
    ads2help, Nov 19, 2008 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    The problem with the looping idea is that 233 is not divisible by 3. Unless you have something else adding to the $names array it most likely isn't a loop.

    Try this to see what's in the final array:

    
    <?php
    
    $names = array("Gobbly", "Loz", "Rayman");
    
    echo "<pre>";
    print_r($names);
    echo "</pre>";
    
    //$namesCount = count($names);
    //print ($namesCount);
    
    
    ?>
    
    PHP:
     
    jestep, Nov 20, 2008 IP
  5. AdultProfiles

    AdultProfiles Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Are you sure that ur script dosnt print other values before this one? Maybe u've forgot an echo or print somewhere.
     
    AdultProfiles, Nov 20, 2008 IP
  6. gobbly2100

    gobbly2100 Banned

    Messages:
    906
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    What you see there is the entire script, it looks as though it is something to do with the strlen() function I am using as if I take that line away it returns the correct value.

    How would that strlen() function be causing it to return 233 I wonder?
     
    gobbly2100, Nov 20, 2008 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    Can you post the code for the script?
     
    jestep, Nov 20, 2008 IP
  8. shineDarkly

    shineDarkly Banned

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hmm, i agree with the posts above, it's pretty straight forward so i cant see any reason why it doesnt work
     
    shineDarkly, Nov 20, 2008 IP