I am trying to pull data from a source, then parse the data for 3 different fields: “title”, “link”, “description” and then output the data. However, the loop appears to load the first 10 articles, but then turns into a never ending loop . How do I fix this?
<?php
#Load in File
$xmlUrl ="http://sports.yahoo.com/mlb/rss.xml";
$ConvertToXml = simplexml_load_file($xmlUrl);
# -> Setup XML
$newsStory = $ConvertToXml->channel->item;
# -----> Load News Stories
for($i = 0;i<10; $i++){
$title=$newsStory[$i]->title;
//$link=$newsStory[$i]->link;
$desc=$newsStory[$i]->description;
echo '<hr>';
echo 'Title:'.$title.'<br />';
//echo 'Link:'.$link.'<br />';
echo 'Description'.$desc.'<br>';
echo '<hr>';
}
?>
The XML I’m parsing:

You have created an endless for-loop:
It is endless because the middle expression is always truthy:
It gives: “Notice: Use of undefined constant i – assumed ‘i'” so it is:
And
"i"is always smaller than10because it evaluates to0in that operator context.Enable error reporting to the highest level on your development platform so that you spot these minor error quickly.
Simple solution is to add the missing
$:But actually in the context of your overall script you might want to prefer a conditional exit: