PHP Function Reference

PHP is_countable() Function



The PHP is_countable() function checks whether a variable is a countable value or not. The function returns true if the variable is a countable value, otherwise it returns false.

Syntax

is_countable(variable)

Parameters

variable Required. Specify the variable being evaluated.

Return Value

Returns true if variable is a countable value, false otherwise.

Example:

The example below shows the usage of is_countable() function.

<?php
$x = array(1, 2, 3);
$y = ['a', 'b', 'c'];
$z = array(10=>'Red', 20=>'Green', 30=>'Blue');

var_dump(is_countable($x));                    //returns: bool(true)
var_dump(is_countable($y));                    //returns: bool(true)
var_dump(is_countable($z));                    //returns: bool(true)
var_dump(is_countable(new ArrayIterator($x))); //returns: bool(true)
var_dump(is_countable(new ArrayIterator()));   //returns: bool(true)

echo "\n";

var_dump(is_countable(10));             //returns: bool(false)
var_dump(is_countable('xyz'));          //returns: bool(false)
var_dump(is_countable(true));           //returns: bool(false)
var_dump(is_countable(new stdClass())); //returns: bool(false)
?>

The output of the above code will be:

bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

bool(false)
bool(false)
bool(false)
bool(false)

❮ PHP Variable Handling Reference