How to check if something is countable?

前端 未结 2 881
余生分开走
余生分开走 2021-01-17 14:05

I have a var: $a. I don\'t know what it is. I want to check if I can count it. Usually, with only array, I can do this:

if (is_array($a)) {
             


        
2条回答
  •  无人及你
    2021-01-17 14:33

    PHP 7.3

    According to the documentation, You can use is_countable function:

    if (is_countable($a)) {
        echo count($a);
    }
    

提交回复
热议问题