How can the count() function be used to replace a for loop in PHP?

Using the count() function in PHP can be a more efficient way to iterate over an array compared to using a for loop. By using count(), you can directly get the number of elements in the array and loop through them without the need for a counter variable or manual incrementing. This can lead to cleaner and more concise code.

$array = [1, 2, 3, 4, 5];

// Using for loop
for($i = 0; $i < count($array); $i++) {
    echo $array[$i] . " ";
}

// Using count() function
foreach($array as $value) {
    echo $value . " ";
}