is_countable()WP 4.9.6

Checks if the content of the variable is a countable value (array or an instance of Countable|SimpleXMLElement|ResourceBundle).

Polyfill for the function is_countable(), which was added in PHP 7.3.

The function is defined only if it does not exist in PHP; otherwise, the native PHP function works.

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.

Returns

true|false. True - countable value, otherwise false.

Usage

is_countable( $var );
$var(mixed) (required)
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.9.1

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