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.
Link to comment
Share on other sites

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
Share on other sites

[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?  :-\
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.