Currently I'm displaying a news feed from an XML file and using a counter to limit the number of news articles that are displayed. But I'm unsure if this is the best way to do it.
Here is my code.
<?php
function get_news($feed) {
$xml = new SimpleXmlElement(uwe_get_file($feed));
$counter = 0;
foreach ($xml->channel->item as $item) {
echo '<a href="' . $item->link . '" target="_blank">' . $item->title . '</a><br />' . '<i>' . $item->description . '</i>' . '<strong>' . $item->pubDate . '</strong>' . '<br />' . '<br />';
++$counter;
if ($counter == 5) {
break;
}
}
}
?>
Any suggestions on how I could improve this counter?
++$counter;
andif($counter == 5)
line toif (++$counter == 5)
\$\endgroup\$if(++$counter === 5) {
\$\endgroup\$echo this . that . the_other
. But unless there's more than one way to skin any cat, and whatever method is going to be most readable in 6 or 12 months is probably the one you should pick. Supporting your code is always more work than writing it. \$\endgroup\$