0

I can't access the count function like PHP 7.1 in PHP 7.2 I need PHP 7.2, but the count function works in older versions of PHP count().

if(count($variable) > 0) {
    // some code you need
}

"message": "count(): Parameter must be an array or an object that implements Countable"

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • You are runnign into this: https://www.php.net/manual/en/migration72.incompatible.php, see count(). This is only a warning, should work like before as of php 7.2. What is $variable? If it is a custom object defined by you, then implement Countable. – g_bor Sep 24 '19 at 12:15
  • In $variable having fetch the record from database. suppose fetch data is empty means return false so that count function is not working. while fetch data not empty means so it's return array, count function working well – Gurumoorthy Dhanabal Sep 25 '19 at 06:16
  • From the doc: Returns the number of elements in array_or_countable. When the parameter is neither an array nor an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned. It seems that the easiest way around would be to have NULL instead of false, otherwise a separate check will be needed. What do you think? – g_bor Sep 25 '19 at 06:31
  • return ( !empty($result) ? $result : FALSE ); – Gurumoorthy Dhanabal Sep 25 '19 at 07:04
  • $result is having data from db. Where and How to change the false into null. Even i tried to change the false into null in that line, but it's not working same error only return. Then i need to change in single file and it apply to every pages, because am used count function multiple files. – Gurumoorthy Dhanabal Sep 25 '19 at 07:14
  • Could you show some code about how the $variable is created? According to the lavarel documentation select always returns an array of results, so the code shuld be working... – g_bor Sep 25 '19 at 07:45
  • case 'select': try { $stmt = $this->connLink->prepare( $Query ); $stmt->execute() or die ('Error in seleting
    '. $Query); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); return ( !empty($result) ? $result : FALSE); } catch( \PDOException $e ) { $this->loadLogType('ERROR_LOG',$Query ); $this->showExceptionAlert($e->getMessage(),$e->getCode(),$Query); die(); } break;
    – Gurumoorthy Dhanabal Sep 25 '19 at 08:03
  • This is my coding – Gurumoorthy Dhanabal Sep 25 '19 at 08:03
  • Do you need the return ( !empty($result) ? $result : FALSE); because of some other reason? Here you are converting the result to FALSE if it is empty. An array with zero elements compares FALSE in a boolean context anyways. Could you just return $result here? – g_bor Sep 25 '19 at 08:10
  • Thanks a lot, it's working for count function, but not working further modules. I got this error Undefined offset: 0 – Gurumoorthy Dhanabal Sep 25 '19 at 09:47
  • I believe that it would not been working even if you supplied FALSE. FALSE does not have an offset 0 either... see here: https://stackoverflow.com/questions/15855346/undefined-offset-0-in-first-php-app – g_bor Sep 25 '19 at 10:02
  • ok, I will check the above link. Thanks a lot for gave me tips – Gurumoorthy Dhanabal Sep 25 '19 at 10:50

0 Answers0