php变量没有更新

时间:2018-03-24 16:01:12

标签: php openweathermap

我是php的新手,我遇到了问题。通过我学校的练习,我必须创建一个使用2 API(及更多)的小天气网站。我正在使用OpenWeatherMap,它正在工作但是当我试图用OpenWeatherAPI给我的图标来充分时,我的变量 $ url_icon $ icon_weather 不是更新

<div><i class="wi <?= $url_icon?>"></i></div>
<div><?= $icon_weather ?></div>

我的PHP

$icon_weather = $data_weather->weather[0]->icon; 
$url_icon = "wi-day-sunny";

if($icon_weather = '01d' || '01n'){
$url_icon = "wi-day-sunny";
} else if($icon_weather = '02d' || '02n'){
$url_icon = "wi-day-cloudy";
} else if($icon_weather = '03d' || '04n'){
$url_icon = "wi-cloud";
} else if($icon_weather = '04d' || '04n'){
$url_icon = "wi-cloudy";
} else if($icon_weather = '09d' || '09n'){
$url_icon = "wi-rain";
} else if($icon_weather = '10d' || '10n'){
$url_icon = "wi-day-rain";
} else if($icon_weather = '11d' || '11n'){
$url_icon = "wi-storm-showers";
} else if($icon_weather = '13d' || '13n'){
$url_icon = "wi-snow-wind";
};

最奇怪的是没有我的if循环(所以没有$ url_icon),icon_weather正在更新:(

谢谢!

2 个答案:

答案 0 :(得分:0)

您的代码应该是以下

 $icon_weather = $data_weather->weather[0]->icon; 
 $url_icon = "wi-day-sunny";

 if($icon_weather == '01d' || $icon_weather == '01n'){
     $url_icon = "wi-day-sunny";
 } else if($icon_weather == '02d' || $icon_weather=='02n'){
     $url_icon = "wi-day-cloudy";
 } else if($icon_weather == '03d' || $icon_weather=='04n'){
     $url_icon = "wi-cloud";
 } else if($icon_weather == '04d' || $icon_weather=='04n'){
     $url_icon = "wi-cloudy";
 } else if($icon_weather == '09d' ||$icon_weather== '09n'){
     $url_icon = "wi-rain";
 } else if($icon_weather == '10d' || $icon_weather=='10n'){
     $url_icon = "wi-day-rain";
 } else if($icon_weather == '11d' || $icon_weather=='11n'){
     $url_icon = "wi-storm-showers";
 } else if($icon_weather == '13d' || $icon_weather=='13n'){
     $url_icon = "wi-snow-wind";
 };

要使用if比较变量,您应该使用==而不是=

答案 1 :(得分:0)

每个if语句都需要使用双等号(==)进行比较。

您的班级文本已经设置为带有空格的wi,然后添加了wi-day-thisval,使其成为wi wi-day-thisval。

你也需要回声才能显示它。

我的个人意见也是

<div><i class="<?php echo $url_icon ?>"></i></div>
<div><?php echo $icon_weather ?></div>