Synergic Posted August 14, 2006 Share Posted August 14, 2006 why do both these functions:sizeof -- Alias of count()count -- Count elements in an array, or properties in an objectgive an array size of 1 even though there's nothing inside it? I then add something to it and it gives me 1 still then 2. what is in index 0? i need to do some manipulation and have to check if an array is empty. Link to comment https://forums.phpfreaks.com/topic/17492-why-does-the-sizeof-and-count-give-incorrect-array-size/ Share on other sites More sharing options...
Chetan Posted August 14, 2006 Share Posted August 14, 2006 Gives me 0 if empty, it dosent give me 1.And if it is a variable then ofcource it gives 1. Link to comment https://forums.phpfreaks.com/topic/17492-why-does-the-sizeof-and-count-give-incorrect-array-size/#findComment-74409 Share on other sites More sharing options...
Synergic Posted August 14, 2006 Author Share Posted August 14, 2006 yeah but if i add something to the array it gives me 1. It's hard to tell if its really empty or not... Link to comment https://forums.phpfreaks.com/topic/17492-why-does-the-sizeof-and-count-give-incorrect-array-size/#findComment-74418 Share on other sites More sharing options...
AndyB Posted August 14, 2006 Share Posted August 14, 2006 The answer is in the manual - http://ca.php.net/manual/en/function.count.php[quote]count() may return 0 for a variable that isn't set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset() to test if a variable is set.[/quote] Link to comment https://forums.phpfreaks.com/topic/17492-why-does-the-sizeof-and-count-give-incorrect-array-size/#findComment-74419 Share on other sites More sharing options...
Synergic Posted August 14, 2006 Author Share Posted August 14, 2006 [code]<? if (isset($_SESSION['cart'])){ echo 'Array is set.';} ?><?echo '<pre>'; print_r( $_SESSION ); echo '</pre>';?>[/code]result:[code]Array is setArray( [cart] => ShoppingCartClass Object ( [items] => Array ( ) [totalPrice] => ))[/code]what gives? :-\ Link to comment https://forums.phpfreaks.com/topic/17492-why-does-the-sizeof-and-count-give-incorrect-array-size/#findComment-74424 Share on other sites More sharing options...
Synergic Posted August 14, 2006 Author Share Posted August 14, 2006 ok fixed, the problem was 'cart' was an object i got confused with 'it' being an array. I accessed the array inside [i] cart [/i] which is [i] items [/i] and recieve a 0 :) Link to comment https://forums.phpfreaks.com/topic/17492-why-does-the-sizeof-and-count-give-incorrect-array-size/#findComment-74444 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.