简体   繁体   English

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

[英]php error at calling a function inside other function

I've write this functions file: 我已经写了这个函数文件:

    <?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]);

    }
  }
?>

But I'm getting the following error which I can't solve: 但是我遇到了以下无法解决的错误:

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

The error points at: $client_coords($client_lat,$client_long) = get_lat_long_from_adress($client_adress); 错误指向: $client_coords($client_lat,$client_long) = get_lat_long_from_adress($client_adress); (this is inside get_closest_disco function) (这在get_closest_disco函数内部)

So is a problem related with the array it returns the function get_lat_long_from_adress($adress). 所以与数组有关的问题是它返回函数get_lat_long_from_adress($ adress)。 I've just followed this link to make this function. 我刚刚按照此链接进行了此功能。

What's wrong? 怎么了? Any ideas? 有任何想法吗? Thank you. 谢谢。

This looks like you are using the wrong syntax to address array elements: 看起来您使用了错误的语法来处理数组元素:

Instead of $client_coords($client_lat,$client_long) simply use $client_coords[$client_lat][$client_long] . 代替$client_coords($client_lat,$client_long)只需使用$client_coords[$client_lat][$client_long]

And by the way you try to assign an array it might be that you try to assign the elements of that array. 顺便说一下,您尝试分配数组的方式可能是您尝试分配该数组的元素 In that case take a look at phps list() expression too. 在这种情况下,还要查看phps list()表达式。

Based on this code: 基于此代码:

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

PHP is making the assumption that $client_coords stores the name of a function, and it thinks you're trying to call it with parameter list ($client_lat,$client_long) . PHP假设$client_coords存储一个函数的名称,并且认为您正在尝试使用参数列表($client_lat,$client_long)调用它。 It then thinks you're trying to assign the result of get_lat_long_from_adress() to the result of that function call. 然后,它认为您正在尝试将get_lat_long_from_adress()的结果分配给该函数调用的结果。

I suspect this is what you meant to do instead: 我怀疑这是您要执行的操作:

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

Using list() lets you assign an array to a group of individual variables. 使用list()可以将数组分配给一组单个变量。

I think you are trying to get the lat and long by addr ,so you can change your function like below 我认为您正在尝试通过addr来获取经度和纬度,因此您可以如下更改函数

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);
      }

and do this to get the result 并这样做以获得结果

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM