简体   繁体   中英

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); (this is inside get_closest_disco function)

So is a problem related with the array it returns the function 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] .

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.

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) . It then thinks you're trying to assign the result of get_lat_long_from_adress() to the result of that function call.

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.

I think you are trying to get the lat and long by addr ,so you can change your function like below

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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