27

The code was working perfectly until when I installed XAMPP 8 (PHP 8).

$size = count($_POST['adm_num']);

But now it's throwing "Argument #1 ($var) must be of type Countable|array, null given" error.

5
  • 3
    You are using count() on non-array. Commented Mar 17, 2021 at 10:34
  • 4
    Avoid the use of the @ operator. Check with isset() or empty(). Commented Mar 17, 2021 at 10:39
  • 3
    Side note: what is the point in defining $size and then overwriting it three times? Commented Mar 17, 2021 at 10:50
  • 1
    @El_Vanja I guess when one of the post parameters is missing, the subsequent one will define the value of $size. Commented Nov 20, 2022 at 12:57
  • Why not try to check whatever $_POST['adm_num'] contains? Commented Jan 3 at 12:09

6 Answers 6

64

In case the array may not exist, you can check its existence and type before counting, and then either set a default value or return an error, if array is expected

if (isset($_POST['adm_num']) && is_countable($aa)) {
    $size = count($_POST['adm_num']);
} else {
    $size = 0;
    // http_response_code(400);
    // die("adm_num must be present and be of type array");
}

In case the array must be defined at this point, this error means that something is broken in your code and you have to debug it, checking the source of this array.

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

1 Comment

Thanks for mentioning is_countable, definitely the most direct solution here!
28

Starting from PHP8.0, using proper type is obligatory for the most function parameters. As a simple solution you may cast a variable to array before counting.

count((array)$XYZVariable);

try this code

$size = count((array)$_POST['adm_num']));

However it may cause unexpected behavior and it's recommended to validate the input value instead, by checking its type and returning an error if validation fails.

Comments

12

Easy Fix ..

$ab = is_array($aa) ? count($aa) : 0 ;

Thanks

Comments

8
@$size = count($_POST['ca1']); 

in PHP 8 will not work you have to do it like that

@$size = count((array)$_POST['ca1']); 

and do this for the rest of it

2 Comments

Downvoted because of the @ Bad practice to suppress errors with that!
In PHP 8.0+ @ operators work differently. Calling it "bad practice" is now more a matter of opinion or for OGs who can't deny the fact that the use cases are actually on point now.
7

For easier code update for PHP8 I made my own function

function count_($array) {
    return is_array($array) ? count($array) : 0;
}

and then I mass replaced count($value) with count_($value)

1 Comment

Be careful with this solution. In my project there are custom count methods like $UsersModel->count() which will absolutely cause problems
1

Quoting from the official php docs for count():

Counts all elements in an array, or something in an object.

The error you're getting is pretty obvious. One of these four variables($_POST['adm_num'], $_POST['ca1'], $_POST['ca2'], $_POST['ca3']) is not an array or maybe more.

You can find out about the type of a variable using gettype(). It'll tell you that which variable does not contains an array. You can than change it to array.

P.s: You're overriding your $size variable three times. Why is that?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.