0

The first line is the problem code. I don't know how to change that count to something that can work.

if(count($item[2]) > 0){
    if($item[2][0] == 'plane' || $item[2][0] == 'url'){
        if($item[2][0] == 'url'){
            $arr = explode('file/d/',$id);
            $arr1 = explode('/',$arr[1]);
            $id = $arr1[0];
        }
   }
 }
?>
1
  • Hi, this is probably caused by the fact that the $item variable is not of countable type and thus the count() function cannot be used. What is inside $item[2] and what does $item hold in the first place? Commented Dec 16, 2019 at 8:50

3 Answers 3

2

In PHP 7.2, a Warning was added while trying to count uncountable things. To make it fix change this line:

if(count($item[2]) > 0){

with this:

if(is_array($item[2]) && count($item[2]) > 0){

In PHP 7.3 a new function was added is_countable, specifically to address the E_WARNING issue. If you are using PHP 7.3 so you can change this line:

if(count($item[2]) > 0){

with this:

if(is_countable($item[2]) && count($item[2]) > 0){
Sign up to request clarification or add additional context in comments.

Comments

0

I believe that in some cases this $item[2] returns null or any other non-countable value. Starting from PHP 7, you will not be able to count an object that not implementing countable. So you need to check first if it's an array:

if(is_countable($item[2])){ // you can also use is_array($item[2])
    if(count($item[2]) > 0){
        //rest of your code
    }
}

Another way (though not preferred) is to pass your object to ArrayIterator. That will make it iteratable:

$item_2 = new ArrayIterator($item[2]);
if(count($item_2) > 0){
   //rest of your code
}

Comments

0

Try code below:

if (is_array($item[2]) || $item[2] instanceof Countable || is_object($item[2])) {
    if(count($item[2]) > 0){
        if($item[2][0] == 'plane' || $item[2][0] == 'url'){
            if($item[2][0] == 'url'){
                $arr = explode('file/d/',$id);
                $arr1 = explode('/',$arr[1]);
                $id = $arr1[0];
            }
        }
    }
}

Check it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.