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. Quote Link to comment 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. Quote Link to comment 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... Quote Link to comment 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] Quote Link to comment 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? :-\ Quote Link to comment 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 :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.