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.
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.
is_countable, definitely the most direct solution here!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.
@$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
@ 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.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)
$UsersModel->count() which will absolutely cause problemsQuoting 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?
count()on non-array.@operator. Check withisset()orempty().$sizeand then overwriting it three times?$_POST['adm_num']contains?