初級 › count()にnullを渡したときのTypeErrorの原因 › パターン1
count()にnullを渡したときのTypeErrorの原因
count()にnullを渡すと、PHP8ではTypeErrorが投げられます。
1 <?php 2 function getItems() { 3 return ____; ^ 4 } 5 $items = getItems(); 6 echo count($items);
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in main.php:6
空欄を埋めて実行を通す
getItems()がnullを返しているため、count()に配列ではなくnullが渡っています。PHP8ではcount(null)はTypeErrorになります。
次の問題