在变量之间添加空格

时间:2012-05-09 15:55:23

标签: php

我正在为课堂做一个项目,我需要在城市州和zip变量之间添加一个空格。请帮忙!谢谢!

代码如下所示:

echo "<p>";
echo $weather['current_observation']['display_location']['city']; 
echo $weather['current_observation']['display_location']['state']; 
echo $weather['current_observation']['display_location']['country']; 
echo $weather['current_observation']['display_location']['zip']; 
echo "</p><p>";
echo $weather['current_observation']['observation_time_rfc822'];
echo "</p><p>";
echo $weather['current_observation']['weather'];
echo "</p><p>";
echo $weather['current_observation']['temperature_string'];
echo "</p><p>";
echo $weather['current_observation']['wind_string'];
echo "</p>";

6 个答案:

答案 0 :(得分:6)

在要添加空格的任何回显线之后

回显echo '&nbsp';

或如通讯中所述:

  

“不需要一个不间断的空间。常规的echo " ";会很好。 - ceejayoz于2012年5月9日16:01”

答案 1 :(得分:3)

怎么样

echo $weather['current_observation']['display_location']['city']; 
echo ' ';
echo $weather['current_observation']['display_location']['state']; 

答案 2 :(得分:2)

你可以这样添加空格

echo $weather['current_observation']['display_location']['city'] . " ";

答案 3 :(得分:1)

您可以使用.

添加到字符串的末尾

所以"foo" . "bar"返回"foobar"

我会写

echo $weather['current_observation']['display_location']['city'] . ' '; 
echo $weather['current_observation']['display_location']['state'] . ' ';

答案 4 :(得分:1)

最简单的方法:

echo " ";

如果那不是诀窍,那么你已经从问题中留下了重要的细节。

答案 5 :(得分:-1)

对于普通空间。

 echo $weather['current_observation']['display_location']['city'] . " ";
 echo $weather['current_observation']['display_location']['state'] . " ";
 echo $weather['current_observation']['display_location']['country'] . " ";
 echo $weather['current_observation']['display_location']['zip'] . " ";

或HTML间距。

 echo $weather['current_observation']['display_location']['city'] . "&nbsp;";
 echo $weather['current_observation']['display_location']['state'] . "&nbsp;";
 echo $weather['current_observation']['display_location']['country'] . "&nbsp;";
 echo $weather['current_observation']['display_location']['zip'] . "&nbsp;";