[英]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_adress($ adress)。 我剛剛按照此鏈接進行了此功能。
怎么了? 有任何想法嗎? 謝謝。
看起來您使用了錯誤的語法來處理數組元素:
代替$client_coords($client_lat,$client_long)
只需使用$client_coords[$client_lat][$client_long]
。
順便說一下,您嘗試分配數組的方式可能是您嘗試分配該數組的元素 。 在這種情況下,還要查看phps list()
表達式。
基於此代碼:
$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()
可以將數組分配給一組單個變量。
我認為您正在嘗試通過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);
}
並這樣做以獲得結果
$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.