仕様
閲覧数:468
投稿日:2024-02-01
更新日:2024-02-01
引数が設定されており、かつ配列またはCountableオブジェクトである場合にその要素数を返し、そうでない場合は0を返します。
引数が NULL の場合には 0 を返しますが、引数を指定しない場合、下記「Fatal error」が発生します。
Fatal error: Uncaught ArgumentCountError: Too few arguments to function countU()
コード
function countU($value) {
if (isset($value) && (is_array($value) || $value instanceof Countable)) {
return count($value);
} else {
return 0;
}
}
$aAry = [];
$bAry = ['あ','い'];
$cStr = 'c';
$dInt = 4;
$fNULL = NULL;
$ra = countU($aAry); //int(0)
$rb = countU($bAry); //int(2)
$rc = countU($cStr); //int(0)
$rd = countU($dInt); //int(0)
$rf = countU($fNULL); //int(0) 結果
int(0) int(2) int(0) int(0) int(0)