count() emitting an E_WARNING

后端 未结 3 1212
-上瘾入骨i
-上瘾入骨i 2020-12-09 20:13

Prior to PHP 7.2 using count() on a scalar value or non-countable object would return 1 or 0.

For example: https://3v4l.org/tGRDE



        
3条回答
  •  隐瞒了意图╮
    2020-12-09 20:22

    You can solve it by using "??"-operator. If the left side is null, the right side will be used. So as we have a empty array, our result will be zero.

    count(null ?? [])
    

    Another way would be to typecast it as an array.

    count((array) null)
    

提交回复
热议问题