The is_countable Function
The is_countable
function checks whether it is possible to count the number of elements in a variable.
It returns true
if the variable is an array or an object implementing
the Countable
interface. Otherwise, it returns false
.
Syntax
is_countable(mixed $value): bool
Example
Let's check an array for the possibility of counting elements:
<?php
$arr = [1, 2, 3];
var_dump(is_countable($arr));
?>
Code execution result:
true
Example
Let's check a string for the possibility of counting elements:
<?php
$str = 'abcde';
var_dump(is_countable($str));
?>
Code execution result:
false
Example
Let's check an object implementing the Countable interface:
<?php
class MyCountable implements Countable {
public function count(): int {
return 5;
}
}
$obj = new MyCountable();
var_dump(is_countable($obj));
?>
Code execution result:
true