函数名称:count() 适用版本:PHP 4, PHP 5, PHP 7 函数描述:count() 函数用于获取数组中元素的个数或对象中属性的个数。 用法示例: 获取数组元素的个数: $frui...
函数名称:count() 适用版本:PHP 4, PHP 5, PHP 7
函数描述:count() 函数用于获取数组中元素的个数或对象中属性的个数。
用法示例:
$fruits = array("apple", "banana", "cherry");
$fruitCount = count($fruits);
echo "数组的元素个数为:" . $fruitCount; // 输出: 数组的元素个数为:3
class Person {
public $name;
public $age;
}
$person = new Person();
$person->name = "John";
$person->age = 30;
$propertyCount = count((array)$person);
echo "对象属性个数为:" . $propertyCount; // 输出: 对象属性个数为:2
注意事项:
(array) 将对象转换为数组。