Jump to content

why does the sizeof and count give incorrect array size?


Synergic

Recommended Posts

why do both these functions:


sizeof -- Alias of count()
count -- Count elements in an array, or properties in an object

give 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.
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]
[code]
<? if (isset($_SESSION['cart']))
{
echo 'Array is set.';
} ?>

<?echo '<pre>';
  print_r( $_SESSION );
echo '</pre>';?>
[/code]

result:

[code]
Array is set

Array
(
    [cart] => ShoppingCartClass Object
        (
            [items] => Array
                (
                )

            [totalPrice] =>
        )

)
[/code]

what gives?  :-\

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.