countDescriptionThis 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. ExampleExample 42. 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
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|