【PHP】计数()备注

PHP的类型处理笔记

执行环境

PHP 7.0x:只需要一种选择

计算()

整理型周边的注意事项。

$array1 = false;
$array2 = 'str';
$array3 = [
    'a',
    'b',
];
$array4 = [
    ['a'],
    ['b'],
];
$array5 = null;

echo count($array1); // 1
echo count($array2); // 1
echo count($array3); // 2
echo count($array4); // 2
echo count($array5); // 0
echo count((array)$array1); // 1
echo count((array)$array2); // 1

根据您的评论,如果类型不正确,PHP7.2将显示警告。
https://www.php.net/manual/ja/migration72.incompatible.php#migration72.incompatible.warn-on-non-countable-types