php在调用其他函数内部的函数时出错


php error at calling a function inside other function

我已经编写了这个函数文件:

    <?php
  function get_num_discos()
  {
      include ('connection.php');
      $result = mysql_query("SELECT * FROM disco", $connect);
      $num_rows = mysql_num_rows($result);
      return $num_rows;
  }  
  function get_disco_name_from_id($id)
  {
    include ('connection.php');
    $query = 'SELECT name FROM disco WHERE id = '.$id;
    $result = mysql_query($query,$connect);
    while ($reg = mysql_fetch_array($result))
        $name = $reg['name'];
    return $name;
  }

  function get_lat_long_from_adress($adress)
  {
    $prepAddr = str_replace(' ','+',$address); 
    $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false'); 
    $output= json_decode($geocode);
    $lat = $output->results[0]->geometry->location->lat;
    $long = $output->results[0]->geometry->location->lng;
    return coords($lat,$long);
  }
  function get_closest_disco($client_adress)
  {
    include ('connection.php');
    $num_discos = get_num_discos;
    $client_adress = "C/ Sant Pau, 70, Lloret de Mar";
    $client_coords($client_lat,$client_long) = get_lat_long_from_adress($client_adress);
    $query = 'SELECT adress FROM disco';
    $result = mysql_query($query,$connect);
    while ($row = mysql_fetch_assoc($result))
    {
        $disco_adress[] = $row; 
    }
    for ($i = 0; i<$num_discos; $i++)
    {
        $disco_coords($disco_lat,$disco_long) = get_lat_long_from_adress($disco_adress[$i]);
    }
  }
?>

但我得到了以下错误,我无法解决:

Fatal error: Can't use function return value in write context in C:'xampp'htdocs'discos_test'functions.php on line 47

错误指向:$client_coords($client_lat,$client_long) = get_lat_long_from_adress($client_adress);(这在get_closest_disco函数内)

与数组相关的问题也是如此,它返回函数get_lat_long_from_address($adress)。我只是通过这个链接来实现这个功能。

怎么了?有什么想法吗?非常感谢。

基于此代码:

$client_coords($client_lat,$client_long) = get_lat_long_from_adress($client_adress);

PHP假设$client_coords存储一个函数的名称,它认为您正试图用参数列表($client_lat,$client_long)来调用它。然后它认为您正试图将get_lat_long_from_adress()的结果分配给该函数调用的结果。

我怀疑这就是你想要做的:

list($client_lat,$client_long) = get_lat_long_from_adress($client_adress);

使用list()可以将数组分配给一组单独的变量。

这看起来像是使用了错误的语法来寻址数组元素:

简单地使用CCD_ 7来代替CCD_。

通过尝试分配数组的方式,可能是尝试分配该数组的元素。在这种情况下,还可以看看phps的list()表达式。

我认为您正在尝试通过addr获取lat和long,所以您可以像下面的一样更改您的函数

function get_lat_long_from_adress($adress)
      {
        $prepAddr = str_replace(' ','+',$address); 
        $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false'); 
        $output= json_decode($geocode);
        $lat = $output->results[0]->geometry->location->lat;
        $long = $output->results[0]->geometry->location->lng;
        return array('lat'=>$lat,'long'=>$long);
      }

这样做可以得到的结果

$client_coords= get_lat_long_from_adress($client_adress);
//do sth about $client_coords['lat']
//do sth about $client_coords['long']