我正在尝试从一些非常基本的Java经验中学习PHP,但我对PHP中的变量感到困惑,并在同一个脚本中重用。虽然我有某些变量的多个值,例如$ url,$ curl,$ resp,但我有以下代码可以在某种程度上无问题地工作?是否只是在脚本执行时被覆盖了?
<?php
$zip = $_GET["ziphtml"];
$url = "http://api.wunderground.com/api/4339efkeyf17a9/forecast10day/q/19115.json";
$curl = curl_init();
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$json_string = $resp;
$parsed_json = json_decode($json_string);
$forecastp2 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[1]->{'title'};
$forecastp3 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[2]->{'title'};
$forecastp4 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[3]->{'title'};
$forecastp5 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[4]->{'title'};
$forecastp6 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[5]->{'title'};
$forecastp7 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[6]->{'title'};
$zip = $_GET["ziphtml"];
$url = "http://api.wunderground.com/api/4dgg345353vdryteyfg339ekey7a9/geolookup/conditions/q/IA/".$zip.".json";
$curl = curl_init();
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$json_string = $resp;
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'current_observation'}->{'display_location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
echo "Current temperatdure in ${location} is: ${temp_f}\n<br /> and it is now ${forecastp2}\n<br /> and then it will be ${forecastp3}\n<br /> and then ${forecastp4}\n<br />and then ${forecastp5}\n<br />and then ${forecastp6}\n<br />and then ${forecastp7}";
?>
答案 0 :(得分:3)
它被覆盖了。
Java也是如此,但Java会检查类型,因此您无法自由地重新分配值。
答案 1 :(得分:1)
变量是内存中某个位置的名称。在该位置存储新值时,旧值将丢失,现在名称将引用新值。