PHP脚本是否可以重用相同的变量


Can a PHP script reuse the same variable?

我试图从一些非常基本的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}";


  ?>

它只是被覆盖。

Java也会这样做,但Java会检查类型,所以您不能自由地重新分配值。

变量是内存中某个位置的名称。当您在该位置存储新值时,旧值将丢失,名称现在引用新值。