What approach should be taken to display the last 5 news titles from an RSS feed using PHP?

To display the last 5 news titles from an RSS feed using PHP, you can use the SimpleXML extension to parse the XML data from the feed. You can then loop through the items and display the titles accordingly. Make sure to handle any errors that may occur during the parsing process.

<?php
$url = 'https://example.com/rss-feed'; // URL of the RSS feed
$xml = simplexml_load_file($url);

if ($xml) {
    $items = $xml->channel->item;
    
    $count = count($items);
    $start = ($count > 5) ? $count - 5 : 0;

    for ($i = $start; $i < $count; $i++) {
        echo $items[$i]->title . "<br>";
    }
} else {
    echo "Failed to load RSS feed.";
}
?>