我的主页包含天气为世界各地的三个城市作为显示的图像增加凝聚力饲料
在主页中,我宣布3个变量存储RSS网址每个城市
$newYorkWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f';
$londonWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=UKXX0085&u=c';
$parisWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=FRXX0076&u=c';
我拉相同的标签上面显示的三个网址,并从传递给它的变量使用3个相同的功能在一处。
下面显示传递给函数的变量。很明显,在$ weather可以返回之前使用其他函数。
function new_york_current_weather($newYorkWeatherSource) {
// Get XML data from source
if (isset($newYorkWeatherSource)) {
$feed = file_get_contents($newYorkWeatherSource);
} else {
echo 'Feed not found. Check URL';
}
checkWeatherFeedExists($feed);
$xml = new SimpleXmlElement($feed);
$weather = get_dateTime($xml);
$weather = get_temperature_and_convert($xml);
$weather = get_conditions($xml);
$weather = get_icon($xml);
return $weather;
}
正如我所提到的,I电流重复这个功能3倍只是替换是在上述实例中通过了$ newYorkWeatherSource变量。任何想法如何我可以重复使用这个功能3次,但通过不同的网址,以保持我的主页显示3个城市的天气?当然,如果每个城市都在个别页面上呈现,但其目的是将它们放在一起以便进行比较,则重用该功能很容易。
任何想法?
在此先感谢。
是的,我想如何重命名函数和变量,但我只是想知道我将如何处理3个RSS URL变量,因为我无法替换全部重命名为$ rss_url,因为我只是覆盖它们,直到最终唯一的URL将成为巴黎,因为它是我的主页上设置的最后一个变量。 – newToJava
我已经用可能对您有帮助的信息更新了我的答案。 – Charles
非常有用,这样简单的修复。非常感谢。投了票。 – newToJava