How can a for loop be used to limit the number of entries displayed from a database in PHP?

To limit the number of entries displayed from a database in PHP, you can use a for loop to iterate over the results fetched from the database query and display only a certain number of entries. By setting a counter variable within the loop and breaking out of the loop once the desired number of entries is reached, you can effectively limit the display.

// Assuming $results contains the data fetched from the database

$limit = 5; // Set the limit to 5 entries

for ($i = 0; $i < count($results) && $i < $limit; $i++) {
    // Display the data from $results array
    echo $results[$i]['column_name'] . "<br>";
}