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

โดย SONGCHAI SAETERN
เจอ Error นี้ใน PHP 7.2 เป็นต้นไป
ตัวอย่างนี้อยู่ในไฟล์ fpdi.php บรรทัดที่ 562

if ($this->state > 2 && count($this->parsers) > 0) {
       foreach ($this->parsers as $k => $_){
.
.
.
.
.


สังเกตว่า count($this->parsers) เป็นการนับ Array เพราะเมื่อเข้าเงื่นไข โค้ดต่อไปก็คือการวนลูปค่าใน Array ดังนั้นก็เพิ่มการตรวจสอบใน if() อีกเงื่อนไขเพิ่มเติม

if ($this->state > 2 && is_array($this->parsers) && count($this->parsers) > 0) {


is_array($this->parsers)  เป็นการเช็คว่าใช่ Array หรือไม่ ถ้าไม่ใช่ก็จะไม่ count() ให้เสียเวลา ปัญหาการ Error ก็จะไม่เกิดขึ้น




หรือจะเขียนยาวขึ้นอีกนิดก็ได้ เพื่อใช้ PHP เวอร์ชั่นอื่นๆได้ด้วย

        $count = 0;
        if (isset($this->parsers) && !empty($this->parsers)){
           $count = count($this->parsers);
        }







PHP CI MANIA - PHP Code Generator 

โปรแกรมช่วยสร้างโค้ด "ลดเวลาการเขียนโปรแกรม"
ราคาสุดคุ้ม  
http://fastcoding.phpcodemania.com