What are some potential pitfalls to avoid when using for-loops in PHP for star rating display?

One potential pitfall when using for-loops in PHP for star rating display is not properly handling the logic for displaying full, half, or empty stars based on the rating value. To avoid this, you can calculate the number of full, half, and empty stars separately within the loop and adjust the display accordingly.

<?php
$rating = 3.5;
$fullStars = floor($rating);
$halfStar = ceil($rating - $fullStars);
$emptyStars = 5 - $fullStars - $halfStar;

for ($i = 0; $i < $fullStars; $i++) {
    echo '<i class="fa fa-star"></i>';
}

if ($halfStar) {
    echo '<i class="fa fa-star-half"></i>';
}

for ($i = 0; $i < $emptyStars; $i++) {
    echo '<i class="fa fa-star-o"></i>';
}
?>