0
0

More than 5 years have passed since last update.

【PHP】count() メモ

Last updated at Posted at 2019-09-09

PHPの型まわりのメモ

実行環境

PHP 7.0x

count()

型まわりの注意点まとめ。

$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でWarningが表示されるようになりました。
https://www.php.net/manual/ja/migration72.incompatible.php#migration72.incompatible.warn-on-non-countable-types

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0