is_countable()WP 4.9.6

Polyfill for is_countable() function added in PHP 7.3.

Verify that the content of a variable is an array or an object implementing the Countable interface.

1 time — 0.000001 sec (speed of light) | 50000 times — 0.01 sec (speed of light) | PHP 7.1.11, WP 4.9.8

No Hooks.

Return

true|false. True if $value is countable, false otherwise.

Usage

is_countable( $value );
$value(mixed) (required)
The value to check.

Examples

0

#1 Check the different data to see if they are countable values.

var_dump( is_countable([1, 2, 3]) );                                // bool(true)
var_dump( is_countable(new ArrayIterator(['foo', 'bar', 'baz'])) ); // bool(true)
var_dump( is_countable(new ArrayIterator()) );                      // bool(true)

var_dump( is_countable(new stdClass()) );                           // bool(false)

Changelog

Since 4.9.6 Introduced.

is_countable() code WP 6.5.3

function is_countable( $value ) {
	return ( is_array( $value )
		|| $value instanceof Countable
		|| $value instanceof SimpleXMLElement
		|| $value instanceof ResourceBundle
	);
}