count
int count(mixed variable)
Returns the number of elements in a variable.
Returns:
Number of elements in the given variable
Description:
This function returns the number of values contained in the variable given by variable
. For scalar variables, this is always 1, unless the variable is unset or contains only NULL, in which case it's 0. For objects, methods are not counted; nor are attributes having no value (attributes with a value of NULL are counted, however). For arrays, all elements having values—even NULL—are counted. Unset elements are not counted. Elements of subarrays are not counted separately, and each subarray counts as one value.
Note:
sizeof() is an alias for count(); they're identical in every way except the name.
Version:
PHP 3, PHP 4
Example:
Count the values contained by variables
$var1 = NULL;
echo "Count of a scalar containing only NULL: " . count($var1) . "\n";
$var2 = 0;
echo "Count of a scalar containing only 0: " . count($var2) . "\n";
class foo {
var $foo1;
var $foo2 = 'something';
function bar() {
return TRUE;
}
}
$var3 = new foo;
echo "Count of the object \$var3: " . count($var3) . "\n";
$var4 = array(1 => NULL, 2 => 1, 3 => 4, array(1, 2, 3, 4));
echo "Count of the array \$var4: " . count($var4) . "\n";
unset($var4[2]);
echo "Count of the array \$var4 after unset(\$var4[2]): " . count($var4) . "\n";
Output:
Count of a scalar containing only NULL: 0
Count of a scalar containing only 0: 1
Count of the object $var3: 1
Count of the array $var4: 4
Count of the array $var4 after unset($var4[2]): 3
|