相关文章推荐
打篮球的鼠标垫  ·  Array.prototype.indexO ...·  1 月前    · 
彷徨的充电器  ·  DAY11 MongoDB ...·  1 年前    · 
淡定的充电器  ·  Pandas ...·  1 年前    · 
任性的柚子  ·  python: import ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, null given in

Ask Question

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

if(isset($_POST["submit"])){
    @$subject = $_POST['subject'];
    @$term = $_POST['term'];
    @$session = $_POST['session'];
    @$size = count($_POST['adm_num']);
    @$size = count($_POST['ca1']);
    @$size = count($_POST['ca2']);
    @$size = count($_POST['ca3']);
    $i = 0;
    while ($i < $size) {
        $ca1= $_POST['ca1'][$i];
        $ca2= $_POST['ca2'][$i];
        $ca3= $_POST['ca3'][$i];
        $adm_num = $_POST['adm_num'][$i];
                @El_Vanja I guess when one of the post parameters is missing, the subsequent one will define the value of $size.
– philler82
                Nov 20, 2022 at 12:57
    @$term = $_POST['term'];
    @$session = $_POST['session'];
    @$size = count((array)$_POST['adm_num']));
    @$size = count((array)$_POST['ca1']));
    @$size = count((array)$_POST['ca2']));
    @$size = count((array)$_POST['ca3']));
    $i = 0;
    while ($i < $size) {
        $ca1= $_POST['ca1'][$i];
        $ca2= $_POST['ca2'][$i];
        $ca3= $_POST['ca3'][$i];
        $adm_num = $_POST['adm_num'][$i];
                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.
– Thielicious
                Sep 1, 2022 at 20:31

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)

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?

In the doc it say :

Return Values Returns the number of elements in value. When the parameter is neither an array nor an object with implemented Countable interface, 1 will be returned. There is one exception, if value is null, 0 will be returned.

It look like it a bug ?!?

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.