Php 可以调用函数两次吗?
每当运行此代码时,我都会得到: PHP致命错误:无法重新声明比较_距离() 临时解决方案似乎是复制Php 可以调用函数两次吗?,php,Php,每当运行此代码时,我都会得到: PHP致命错误:无法重新声明比较_距离() 临时解决方案似乎是复制findNearestLocation()并给它一个不同的名称,因此我没有调用findNearestLocation()两次,而是调用findNearestLocation1()和findNearestLocation2() 我尝试在foreach循环中取消设置变量,但我不能完全弄清楚我缺少了什么。结果证明,相对简单的解决方案是不在函数中声明函数。。一旦我将compare_distance()移动到
findNearestLocation()
并给它一个不同的名称,因此我没有调用findNearestLocation()
两次,而是调用findNearestLocation1()
和findNearestLocation2()
我尝试在foreach循环中取消设置变量,但我不能完全弄清楚我缺少了什么。结果证明,相对简单的解决方案是不在函数中声明函数。。一旦我将
compare_distance()
移动到findNearestLocation()
函数之外,它就可以正常工作了
所以现在我明白了什么,但我真的不明白为什么。。在函数中声明函数会产生什么问题
抱歉,如果这似乎是一个明显的问题,我对PHP还是相当陌生的
<?php
function findNearestLocation($clientLocation) {
global $depotLocations;
$findDepot = $depotLocations;
$count = 0;
foreach ($findDepot as $row) {
$currentDepot = array($row['location_latitude'], $row['location_longitude']);
$findDepot[$count]['distance'] = distance($currentDepot,$clientLocation);
$count ++;
}
function compare_distance($a, $b) {
if ($a['distance'] == $b['distance']) return 0;
return ($a['distance'] < $b['distance']) ? -1 : 1;
}
usort($findDepot, 'compare_distance1');
return $findDepot[0];
}
function distance($depot, $clientLocation) { // need to send two arrays with only longitude and latitude
list($lat1, $lon1) = $depot;
list($lat2, $lon2) = $clientLocation;
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$kilometres = $dist * 60 * 1.1515 * 1.609344;
return $kilometres;
}
// 4. ** These next two lines cause the error. If I comment out either of these lines, the code works fine and returns the expected result.
$finalPickup = findNearestLocation($pickupLocation);
$finalDropoff = findNearestLocation($dropoffLocation);
var_dump($finalPickup);
var_dump($finalDropoff);
?>
来源:相对简单的解决方案是不在函数中声明函数。。一旦我将
compare_distance()
移动到findNearestLocation()
函数之外,它就可以正常工作了
所以现在我明白了什么,但我真的不明白为什么。。在函数中声明函数会产生什么问题
抱歉,如果这似乎是一个明显的问题,我对PHP还是相当陌生的
<?php
function findNearestLocation($clientLocation) {
global $depotLocations;
$findDepot = $depotLocations;
$count = 0;
foreach ($findDepot as $row) {
$currentDepot = array($row['location_latitude'], $row['location_longitude']);
$findDepot[$count]['distance'] = distance($currentDepot,$clientLocation);
$count ++;
}
function compare_distance($a, $b) {
if ($a['distance'] == $b['distance']) return 0;
return ($a['distance'] < $b['distance']) ? -1 : 1;
}
usort($findDepot, 'compare_distance1');
return $findDepot[0];
}
function distance($depot, $clientLocation) { // need to send two arrays with only longitude and latitude
list($lat1, $lon1) = $depot;
list($lat2, $lon2) = $clientLocation;
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$kilometres = $dist * 60 * 1.1515 * 1.609344;
return $kilometres;
}
// 4. ** These next two lines cause the error. If I comment out either of these lines, the code works fine and returns the expected result.
$finalPickup = findNearestLocation($pickupLocation);
$finalDropoff = findNearestLocation($dropoffLocation);
var_dump($finalPickup);
var_dump($finalDropoff);
?>
资料来源:当然可以,伙计)
更改:
function findNearestLocation($clientLocation) {
global $depotLocations;
$findDepot = $depotLocations;
$count = 0;
foreach ($findDepot as $row) {
$currentDepot = array($row['location_latitude'], $row['location_longitude']);
$findDepot[$count]['distance'] = distance($currentDepot,$clientLocation);
$count ++;
}
function compare_distance($a, $b) {
if ($a['distance'] == $b['distance']) return 0;
return ($a['distance'] < $b['distance']) ? -1 : 1;
}
usort($findDepot, 'compare_distance');
return $findDepot[0];
}
函数findNearestLocation($clientLocation){
全球$depotlocation;
$findDepot=$depotLocations;
$count=0;
foreach($findDepot作为$row){
$currentDepot=数组($row['location\u latitude'],$row['location\u latitude']);
$findDepot[$count]['distance']=distance($currentDepot,$clientLocation);
$count++;
}
函数比较距离($a,$b){
如果($a['distance']=$b['distance'])返回0;
回报($a['distance']<$b['distance'])?-1:1;
}
usort($findDepot,'compare_distance');
返回$findDepot[0];
}
致:
函数比较\u距离($a,$b){
如果($a['distance']=$b['distance'])返回0;
回报($a['distance']<$b['distance'])?-1:1;
}
函数findNearestLocation($clientLocation){
全球$depotlocation;
$findDepot=$depotLocations;
$count=0;
foreach($findDepot作为$row){
$currentDepot=数组($row['location\u latitude'],$row['location\u latitude']);
$findDepot[$count]['distance']=distance($currentDepot,$clientLocation);
$count++;
}
usort($findDepot,比较距离);
返回$findDepot[0];
}
或(如果您使用的是PHP7):
函数findNearestLocation($clientLocation){
全球$depotlocation;
$findDepot=$depotLocations;
$count=0;
foreach($findDepot作为$row){
$currentDepot=数组($row['location\u latitude'],$row['location\u latitude']);
$findDepot[$count]['distance']=distance($currentDepot,$clientLocation);
$count++;
}
乌斯波特(
$findDepot,
函数($a,$b){返回$a['distance']$b['distance'];}
);
返回$findDepot[0];
}
祝你好运 当然,伙计)
更改:
function findNearestLocation($clientLocation) {
global $depotLocations;
$findDepot = $depotLocations;
$count = 0;
foreach ($findDepot as $row) {
$currentDepot = array($row['location_latitude'], $row['location_longitude']);
$findDepot[$count]['distance'] = distance($currentDepot,$clientLocation);
$count ++;
}
function compare_distance($a, $b) {
if ($a['distance'] == $b['distance']) return 0;
return ($a['distance'] < $b['distance']) ? -1 : 1;
}
usort($findDepot, 'compare_distance');
return $findDepot[0];
}
函数findNearestLocation($clientLocation){
全球$depotlocation;
$findDepot=$depotLocations;
$count=0;
foreach($findDepot作为$row){
$currentDepot=数组($row['location\u latitude'],$row['location\u latitude']);
$findDepot[$count]['distance']=distance($currentDepot,$clientLocation);
$count++;
}
函数比较距离($a,$b){
如果($a['distance']=$b['distance'])返回0;
回报($a['distance']<$b['distance'])?-1:1;
}
usort($findDepot,'compare_distance');
返回$findDepot[0];
}
致:
函数比较\u距离($a,$b){
如果($a['distance']=$b['distance'])返回0;
回报($a['distance']<$b['distance'])?-1:1;
}
函数findNearestLocation($clientLocation){
全球$depotlocation;
$findDepot=$depotLocations;
$count=0;
foreach($findDepot作为$row){
$currentDepot=数组($row['location\u latitude'],$row['location\u latitude']);
$findDepot[$count]['distance']=distance($currentDepot,$clientLocation);
$count++;
}
usort($findDepot,比较距离);
返回$findDepot[0];
}
或(如果您使用的是PHP7):
函数findNearestLocation($clientLocation){
全球$depotlocation;
$findDepot=$depotLocations;
$count=0;
foreach($findDepot作为$row){
$currentDepot=数组($row['location\u latitude'],$row['location\u latitude']);
$findDepot[$count]['distance']=distance($currentDepot,$clientLocation);
$count++;
}
乌斯波特(
$findDepot,
函数($a,$b){返回$a['distance']$b['distance'];}
);
返回$findDepot[0];
}
祝你好运 我对PHP语言的总体理解是,(耸耸肩),它从未如此复杂。它真的不知道“嵌套函数”,它实际上只有两个变量作用域。等等 (但话说回来,它的设计者从来没有真正着手创造“世界上最流行的网络编程语言!”所以它就这么说了……)
最初的编译器错误消息:
无法重新声明{foobar},
是您的第一个指示,表明该语言没有“摸索”您试图做的事情。我对PHP语言的一般理解是,(耸耸肩),它从未如此复杂。它真的不知道“嵌套函数”,它实际上只有两个变量作用域。等等
(但话说回来,它的设计者从来没有真正着手创造“世界上最流行的网络编程语言!”所以它就这么说了……)
初始