Php 在return语句之前移动foreach循环

Php 在return语句之前移动foreach循环,php,Php,我试图将天气预报函数的结果放在PHP返回语句中。要使用串联实现这一点,我需要在代码中向上移动一个foreach循环,但我无法让它工作 我正在尝试创建这样一个函数:: function getWeatherForecast($atts) { all variables go here; return 'concatenated output'; } 下面是完整脚本的链接: 这就是我现在拥有的: function getWeatherForecast($atts) { $x

我试图将天气预报函数的结果放在PHP返回语句中。要使用串联实现这一点,我需要在代码中向上移动一个foreach循环,但我无法让它工作

我正在尝试创建这样一个函数::

function getWeatherForecast($atts) {
    all variables go here;
    return 'concatenated output';
}
下面是完整脚本的链接:

这就是我现在拥有的:

function getWeatherForecast($atts) {
    $xml = simplexml_load_file('http://www.google.com/ig/api?weather=barcelona');
    $information = $xml->xpath("/xml_api_reply/weather/forecast_information");
    $current = $xml->xpath("/xml_api_reply/weather/current_conditions");
    $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");

    /* The foreach loop should go here */

    return '<h2>The weather today ' . $information[0]->city['data'] . '</h2>';
    /* etc. etc. */
}
函数getWeatherForecast($atts){
$xml=simplexml\u加载\u文件('http://www.google.com/ig/api?weather=barcelona');
$information=$xml->xpath(“/xml\u api\u reply/weather/forecast\u information”);
$current=$xml->xpath(“/xml\u api\u reply/weather/current\u conditions”);
$forecast\u list=$xml->xpath(“/xml\u api\u reply/weather/forecast\u conditions”);
/*foreach循环应该在这里*/
返回“今日天气”。$information[0]->city['data']”;
/*等等等等*/
}
这是我需要放在return语句之前的foreach循环:

<?php foreach ($forecast_list as $forecast) : ?>
<div>
    <img src="<?php echo 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?>
    <div><?php echo $forecast->day_of_week['data']; ?></div>
    <span>
    <?php echo $forecast->low['data'] ?>&deg; F – <?php echo $forecast->high['data'] ?>&deg; F,
    <?php echo $forecast->condition['data'] ?>
    </span>    
</div>
<?php endforeach ?>

图标['data']?>“alt=”天气“?>
&华氏度–华氏度,

非常感谢您的帮助!!

我们很清楚,您希望foreach位于函数内部,对吗

在这种情况下:

 $html_code = "";
 foreach ($forecast_list as $forecast) {
     $html_code .='<div>
          <img src="http://www.google.com' . $forecast->icon['data']. '" alt="weather" />
          <div>'.$forecast->day_of_week['data'].'</div>
          <span>
              '. $forecast->low['data'] .'&deg; F – '. $forecast->high['data'] .'&deg; F,
            '. $forecast->condition['data'] .'
         </span>    
         </div>
  }
$html_code=”“;
foreach($forecast\u列表为$forecast){
$html_代码。='
图标['data']。“alt=”天气“/>
“.$forecast->day of_of_week['data']”
“.$forecast->low['data'].°;F–”.$forecast->high['data'].°;F,
“.$forecast->condition['data']”
}
然后,$html_代码将在return语句中包含所有需要连接的html代码


这就是你需要的吗?

我试过这个,效果很好:

<?
    echo getWeatherForecast('');
    function getWeatherForecast($atts) {
        $xml = simplexml_load_file('http://www.google.com/ig/api?weather=barcelona');
        $information = $xml->xpath("/xml_api_reply/weather/forecast_information");
        $current = $xml->xpath("/xml_api_reply/weather/current_conditions");
        $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
        foreach ($forecast_list as $forecast) { 
?>
    <div>
        <img src="<?php echo 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?>
        <div><?php echo $forecast->day_of_week['data']; ?></div>
        <span>
            <?php echo $forecast->low['data'] ?>&deg; F – <?php echo $forecast->high['data'] ?>&deg; F,
            <?php echo $forecast->condition['data'] ?>
        </span>    
    </div>
<?  }

    return '<h2>The weather today ' . $information[0]->city['data'] . '</h2>';
    /* etc. etc. */
    }
?>

图标['data']?>“alt=”天气“?>
&华氏度–华氏度,
但这不是正确的方法。使用一个变量累积html,然后返回所有html。如下所示:

<?
    echo getWeatherForecast('');
    function getWeatherForecast($atts) {
        $xml = simplexml_load_file('http://www.google.com/ig/api?weather=barcelona');
        $information = $xml->xpath("/xml_api_reply/weather/forecast_information");
        $current = $xml->xpath("/xml_api_reply/weather/current_conditions");
        $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
        $html = '';
        foreach ($forecast_list as $forecast) {
            $html .= '<div>';
            $html .=  '<img src="http://www.google.com'.$forecast->icon['data'].'" alt="weather"/>';
            $html .=  '<div>'.$forecast->day_of_week['data'].'</div>';
            $html .=  '<span>';
            $html .=  $forecast->low['data'].'&deg; F – '.$forecast->high['data'].'&deg; F,'.$forecast->condition['data'];
            $html .=  '</span>';
            $html .= '</div>';
        }
        $html .= '<h2>The weather today ' . $information[0]->city['data'] . '</h2>'

        return $html;
        /* etc. etc. */
    }
?>

有什么问题,您无法关闭
?>
并打开