Jump to content

Warning: count(): Parameter must be an array or an object


jarvis

Recommended Posts

Good morning,

I've the following code:

    $message = '';
    if ('delete' === $table->current_action()) {
        $message = '<p>' . sprintf(__('Notifications deleted: %d', 'prg_customer_notification'), count($_REQUEST['id'])) . '</p>';
    }

Which seems to cause a warning of:

Quote

Warning: count(): Parameter must be an array or an object that implements Countable in

When I've had similar before, it's been a case of doing something like changing:

if (count($_REQUEST['id']) > 0) :

To:

if (is_countable($_REQUEST['id']) && count($_REQUEST['id']) > 0) :

But I can't fathom out how to apply the same principal to the above code. 

Or have I misunderstood the warning?

Link to comment
Share on other sites

all that did was hide the actual problem.

if your code is expecting an array of data as its input, it's an application error if it isn't. either a programming mistake or an incorrect user action (directly requesting a delete action page without first visiting the page selecting what to delete) caused the expected input to not be the expected data type. for programming mistakes, you need to find an fix the root cause of the problem. for an incorrect user action, you need to setup and display a message telling the user either what they did wrong, i.e. you cannot directly request this page, or what they need to do to correct the problem, i.e. you must select at least one notification to delete.

  • Like 1
Link to comment
Share on other sites

If you step back from the minutae for a second and look at what your code is doing:

 

if ('delete' === $table->current_action()) {
   $message = '<p>' . sprintf(__('Notifications deleted: %d', 'prg_customer_notification'), count($_REQUEST['id'])) . '</p>';
}

This has an implicit assumption built into it:  so long as the current_action() result is the string 'delete' then you assume that there was notifications deleted.  

  • What if there were no notifications deleted, or the list to be deleted was empty?

So how could you fairly easily fix this issue, where you need to be sure that there is a reason to display this message, while also providing additional feedback to the UI?

if ('delete' === $table->current_action()) {
    if (!empty($_REQUEST['id']) && is_array($_REQUEST['id'])) {
        $message = '<p>' . sprintf(__('Notifications deleted: %d', 'prg_customer_notification'), count($_REQUEST['id'])) . '</p>';
    } else {
        $message = '<p>Nothing to delete.</p>';
    }
}

 

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.