How can the PHP code be modified to display only the latest 3 or 5 news entries?

To display only the latest 3 or 5 news entries, you can modify the PHP code by adding a limit parameter to the SQL query that retrieves the news entries. By specifying the limit as 3 or 5, you can control the number of entries displayed on the page.

// Connect to the database
$pdo = new PDO("mysql:host=localhost;dbname=news_db", "username", "password");

// Retrieve the latest 3 news entries
$stmt = $pdo->prepare("SELECT * FROM news_entries ORDER BY date_published DESC LIMIT 3");
$stmt->execute();
$news_entries = $stmt->fetchAll();

// Display the news entries
foreach ($news_entries as $entry) {
    echo "<h2>{$entry['title']}</h2>";
    echo "<p>{$entry['content']}</p>";
}