Home  >  Q&A  >  body text

Use PHP to write a function about getting weather conditions for a specified number of days. The function section is normal, but when it is added to the for loop, there is no display or error. How to solve it?

Reference materials:
Sina weather api description
Brief description The last parameter in the url, day=0, represents today, if equal to 1, it represents the next day, and so on, but The maximum value is 3.

Code Target:

Jinhua Weather

Today xx-xx-xx Sunday x Sunny to cloudy during the day xxxx

Week x Sunny to cloudy during the day xxxx at night (Day 2)

Sunday x Sunny to cloudy during the day and xxxx at night (Day 3)

Sunday x Sunny to cloudy during the day and xxxx at night (Day 4)

<?php 
function get_weather($k=3){
 $arr = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六','星期日','星期一','星期二');
 $week=date('w');
 $arrfeng=array('无风','软风','轻风','微风','和风','轻劲风','强风','疾风','大风','烈风','狂风','暴风','台风','风王之王');
 $winfo='金华天气';
for ($i=0; $i>$k ; $i++) { 
          $url='http://php.weather.sina.com.cn/xml.php?city=%bd%f0%bb%aa&password=DJOYnieT8234jlsK&day='.$i;
          $ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          $output = curl_exec($ch);
curl_close($ch);
          $outobj=simplexml_load_string($output);
          $a=$outobj->Weather;
//为了方便拼接字符串
          $b=$a->status1;//天气情况1代表白天
          $c=$a->status2;//天气情况2代表晚上
          $d=$a->direction1;//白天风向
          $e=$a->direction2;//晚上风向
          $dd=$a->power1;//白天风级数
          $ee=$a->power2;//晚上风级数
if($c=="")$c=$b;//解决 当晚上和白天 天气一样时 变量ee值为空
             //$winfo='金华天气'
              $winfo.= $arr[$week+$k].'白天'.$b.$d.$arrfeng[intval($dd)].'晚上'.$c.$e.$arrfeng[intval($ee)].'\n';
          }
return $winfo;
}
$str=get_weather(3);
echo $str;
?>

But if you don’t add a for loop, you can achieve it with a solid line by executing the function 4 times. code show as below

<?php 
function get_weather($i){
 $arr = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六','星期日','星期一','星期二');
 $week=date('w');
 $arrfeng=array('无风','软风','轻风','微风','和风','轻劲风','强风','疾风','大风','烈风','狂风','暴风','台风','风王之王');
 $winfo='  ';
          $url='http://php.weather.sina.com.cn/xml.php?city=%bd%f0%bb%aa&password=DJOYnieT8234jlsK&day='.$i;
          $ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          $output = curl_exec($ch);
curl_close($ch);
          $outobj=simplexml_load_string($output);
          $a=$outobj->Weather;
//为了方便拼接字符串
          $b=$a->status1;//天气情况1代表白天
          $c=$a->status2;//天气情况2代表晚上
          $d=$a->direction1;//白天风向
          $e=$a->direction2;//晚上风向
          $dd=$a->power1;//白天风级数
          $ee=$a->power2;//晚上风级数
if($c=="")$c=$b;//解决 当晚上和白天 天气一样时 变量ee值为空
             //$winfo='金华天气';原本在这里(每循环一次就被清空一次这是错误的)
              $winfo.= $arr[$week+$i].'白天'.$b.$d.$arrfeng[intval($dd)].'晚上'.$c.$e.$arrfeng[intval($ee)].'\n';
          
return $winfo;
}
$str0=get_weather(0);
$str1=get_weather(1);
$str2=get_weather(2);
$str3=get_weather(3);
echo '金华天气'.$str0.$str1.$str2.$str3;
?>

The output is as follows:

Jinhua Weather Friday Thundershowers during the day, east wind calm, cloudy at night, east wind calm n Saturday, thundershowers during the day, east wind calm, evening cloudy east wind, calm n Sunday, cloudy during the day, east wind calm, evening clear, east wind calm, n Monday clear during the day, east wind calm, clear at night No wind n

女神的闺蜜爱上我女神的闺蜜爱上我2635 days ago806

reply all(2)I'll reply

  • 阿神

    阿神2017-07-03 11:42:37

    The judgment condition of your for loop is wrong. How is it possible to write $i>$k, and then let $i++.

    <?php 
    function get_weather($k=3){
     $arr = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六','星期日','星期一','星期二');
     $week=date('w');
     $arrfeng=array('无风','软风','轻风','微风','和风','轻劲风','强风','疾风','大风','烈风','狂风','暴风','台风','风王之王');
     $winfo='金华天气';
    for ($i=0; $i<=$k ; $i++) { 
              $url='http://php.weather.sina.com.cn/xml.php?city=%bd%f0%bb%aa&password=DJOYnieT8234jlsK&day='.$i;
              $ch=curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              $output = curl_exec($ch);
    curl_close($ch);
              $outobj=simplexml_load_string($output);
              $a=$outobj->Weather;
    //为了方便拼接字符串
              $b=$a->status1;//天气情况1代表白天
              $c=$a->status2;//天气情况2代表晚上
              $d=$a->direction1;//白天风向
              $e=$a->direction2;//晚上风向
              $dd=$a->power1;//白天风级数
              $ee=$a->power2;//晚上风级数
    if($c=="")$c=$b;//解决 当晚上和白天 天气一样时 变量ee值为空
                 //$winfo='金华天气'
                  $winfo.= $arr[$week+$k].'白天'.$b.$d.$arrfeng[intval($dd)].'晚上'.$c.$e.$arrfeng[intval($ee)].'\n';
              }
    return $winfo;
    }
    $str=get_weather(3);
    echo $str;
    ?>

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-07-03 11:42:37

    The for loop condition is written wrong, it should be $i<=$k

    reply
    0
  • Cancelreply