Re: Bug #6153: count() function not returning proper value

From: Date: Mon, 14 Aug 2000 20:34:24 +0000
Subject: Re: Bug #6153: count() function not returning proper value
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-28856@lists.php.net to get a copy of this message
I'm not sure I understand what you mean, but I think the root of your error is that you think that arrays in PHP are continuous and in one block, which they aren't. count() does not return the 'length' of an array, because this has no meaning in PHP. Instead, it returns the number of elements in the array. For instance, $foo[5] = "test"; Will create an array with a single element. count($foo) will return 1, and not 5 (nor 6). Please don't submit 3 entries for the same bug report... Zeev At 22:28 14/08/2000, jdoyon@nrcan.gc.ca wrote:
From:             jdoyon@nrcan.gc.ca
Operating system: Linux RedHat 6.2
PHP version:      3.0.15
PHP Bug Type:     Misbehaving function
Bug description: count() function not returning proper value I just discovered that the count() function to return the length of an array does not seem to be returning the proper value. I have an array of length 858, and it keeps telling me it's 464 ! If I manually force the loop I can output all elements, whereas if I use a for() loop with "$i < count($array)" as I usually do, I get only part of it ! I suspect this maybe caused by the fact that this is an array of counters, where not all single elements might get initialized. (i.e. some counters increase, some remain NULL). Here is the code that gave up the problem : <? $keywords = $HTTP_POST_VARS["keywords"]; $lang = $HTTP_POST_VARS["lang"]; $prov = $HTTP_POST_VARS["prov"]; preg_match("/\w+/",$keywords,$words); $dbm = dbmopen("search_index.db","r"); $recs = array(); for ( $i = 0 ; $i < count($words) ; $i++ ) {
        if ( dbmexists($dbm,$words[$i]) ) {
                $recs = dbmfetch($dbm,$words[$i]);
                $recar = split("-",$recs);
                for ( $ii = 0 ; $ii < count($recar) ; $ii++ ) {
                        $counter[$recar[$ii]]++;
                }
        }
} for ( $i = 0 ; $i < 1000 ; $i++ ) {
        echo $i." : ".$counter[$i]."<BR>";
} ?> -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net For additional commands, e-mail: php-dev-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net
-- Zeev Suraski <zeev@zend.com> http://www.zend.com/

« previous php.dev (#28856) next »