在PHP中,sizeof() 函数用于返回数组或对象的长度或元素个数。以下是一些正确使用 sizeof() 函数的示例:
$array = array(1, 2, 3, 4, 5);
$length = sizeof($array);
echo $length; // Output: 5
$assoc_array = array('name' => 'John', 'age' => 30, 'city' => 'New York');
$length = sizeof($assoc_array);
echo $length; // Output: 3
class Person {
public $name = 'John';
public $age = 30;
public $city = 'New York';
}
$person = new Person();
$length = sizeof((array) $person);
echo $length; // Output: 3
请注意,sizeof() 函数是 count() 函数的别名,因此也可以使用 count() 来获得数组或对象的长度。