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