Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, null given in
You upgraded your stack to XAMPP 8 and suddenly count($_POST['adm_num']) is throwing a fatal error instead of returning an integer. The message is explicit: "Argument #1 ($var) must be of type Countable|array, null given." This isn't a bug; it's a deliberate change in behavior introduced with PHP 8 that catches a class of bugs that used to run silently or crash your script in less predictable ways.
In older versions of PHP, if you attempted to count() a non-existent array index, the engine would often treat it as an empty array, returning 0. While this prevented crashes, it made debugging logic errors extremely difficult because your code was assuming data existed when it didn't. PHP 8 removed this silent assumption. Now, if you access an index that doesn't exist on a superglobal like $_POST, the result is strictly null. Passing null to count() violates the strict typing rules introduced in the language, triggering the TypeError immediately.
The fix requires adjusting how you interact with superglobals. You need to ensure the variable you are counting is actually an array before passing it to the function. The most robust pattern involves checking if the key exists first.
$size = 0;
if (isset($_POST['adm_num'])) {
$size = count($_POST['adm_num']);
}
This approach explicitly handles the case where the form field was not submitted or was empty. If $_POST['adm_num'] is missing, the code skips the counting logic entirely and defaults to a safe value. This prevents the TypeError from halting execution and allows your application to proceed with a known state.
Alternatively, you can cast the result of accessing the array key to an array using the splat operator or []. However, simply casting null to an array in PHP 8 does not convert it into an empty array; it still results in null. Therefore, the most reliable method remains checking for the existence of the key or initializing a fallback variable.
$size = count($_POST['adm_num'] ?? []);
The null coalescing operator (??) provides a clean one-liner solution. If $_POST['adm_num'] evaluates to false (which includes null and an empty array), it substitutes with the right-hand side, which is an empty array []. Counting an empty array returns 0, maintaining backward compatibility with the behavior you likely had in PHP 7 while adhering to PHP 8's type safety.
It is crucial to remember that superglobals like $_POST are not associative arrays; they are specifically typed as such, but accessing a missing key yields null, not an empty array. This distinction is the root cause of the error. When migrating code from older environments or upgrading frameworks like Laravel to PHP 8.x, always assume that data access might return null if the input is absent.
For more complex scenarios where you are working with user inputs directly, consider validating and sanitizing the entire $_POST payload before processing specific fields. This defensive programming style ensures that your application logic remains stable regardless of how the client sends the request. If you are building a larger application, leveraging a framework's built-in validation middleware can handle these checks automatically before your controller logic even runs, preventing TypeErrors from bubbling up to the core application flow.
