php 变量没有更新

php variable isn't updating

我是 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 更新得很好 :(

谢谢!

你的代码应该如下

 $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 比较变量,您应该使用 == 而不是 =

您的每个 if 语句都需要有双等号 (==) 才能进行比较。

您的 class 文本已设置为带 space 的 wi,然后添加 wi-day-thisval 使其成为 wi wi-day-thisval.

您还需要回显才能显示。

我个人的看法也是

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