2

I populate a whole bunch of data into an array (either by pulling in from memcache or querying the database). I want to count how many rows are in this array to make sure it's greater than 0. When I use count(), it returns nothing, even when the array is full of data.

For troubleshooting, I tried the following code:

$item_count=count($item_data);
print_r($item_data);
die($item_count);

This prints out a huge array that I can see has 146 elements. But $item_count is NULL. Any idea what could be wrong? Thanks!

As requested in the comments, here's an example of what the array looks like that I'm trying to count:

Array
(
    [0] => Array
        (
            [calories] => 190
            [healthy_option] => 0
            [name] => Chicken McNuggets
            [url_name] => chicken-mcnuggets
            [category_name] => Chicken Nuggets and Strips
            [category_url_name] => chicken-nuggets-strips
            [category_id] => 85
        )

    [1] => Array
        (
            [calories] => 380
            [healthy_option] => 0
            [name] => Chicken Selects Premium Breast Strips - 3 piece
            [url_name] => chicken-selects-premium-breast-strips-3-piece
            [category_name] => Chicken Nuggets and Strips
            [category_url_name] => chicken-nuggets-strips
            [category_id] => 85
        )

)
4
  • 1
    I don't believe count can return null. Are you sure you're not mis-spelling something? If not, please provide replication example data. Commented Jun 9, 2017 at 14:05
  • Try an echo $item_count or a var_dump($item_count); Commented Jun 9, 2017 at 14:09
  • Can't replicate using the example data provided. Have a go on 3v4l.org and see if you can. Commented Jun 9, 2017 at 14:13
  • die("$item_count"); will work correctly Commented Jun 9, 2017 at 14:30

1 Answer 1

3

The die() function behaves like exit(). This means that an Integer argument is considered as a "status".

From the doc:

If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.

If you want to print a value, I recommend you to use something else like echo, print_r, etc.

Do you have the same troubles without using die() ?

Sign up to request clarification or add additional context in comments.

1 Comment

Nice catch, completely missed that one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.