3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** *求两个已知经纬度之间的距离,单位为米 *@param lng1,lng2 经度 *@param lat1,lat2 纬度 *@return float 距离,单位米 *@author www.phpernote.com **/ function getdistance($lng1,$lat1,$lng2,$lat2){ //将角度转为狐度 $radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度 $radLat2=deg2rad($lat2); $radLng1=deg2rad($lng1); $radLng2=deg2rad($lng2); $a=$radLat1-$radLat2; $b=$radLng1-$radLng2; $s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137*1000; return $s; } echo getdistance(31.2014966,121.40233369999998,31.22323799999999,121.44552099999998); function calculateSignDistance($longitude1, $latitude1, $longitude2, $latitude2) { $lat1 = ($latitude1 * pi() ) / 180; $lng1 = ($longitude1 * pi() ) / 180; $lat2 = ($longitude2 * pi() ) / 180; $lng2 = ($latitude2 * pi() ) / 180; $calcLongitude = $lng2 - $lng1; $calcLatitude = $lat2 - $lat1; $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2); $stepTwo = 2 * asin(sqrt($stepOne)); return 6378.137*1000 * $stepTwo; } echo "\n"; echo calculateSignDistance(31.2014966,121.40233369999998,31.22323799999999,121.44552099999998); /** *求两个已知经纬度之间的距离,单位为千米 *@param lng1,lng2 经度 *@param lat1,lat2 纬度 *@return float 距离,单位千米 **/ function distance($lng1,$lat1,$lng2,$lat2)//根据经纬度计算距离 { //将角度转为弧度 $radLat1=deg2rad($lat1); $radLat2=deg2rad($lat2); $radLng1=deg2rad($lng1); $radLng2=deg2rad($lng2); $a=$radLat1-$radLat2;//两纬度之差,纬度<90 $b=$radLng1-$radLng2;//两经度之差纬度<180 $s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137*1000; return $s; } echo "\n"; echo distance(31.2014966,121.40233369999998,31.22323799999999,121.44552099999998); function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) { $theta = $longitude1 - $longitude2; $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); $miles = acos($miles); $miles = rad2deg($miles); $miles = $miles * 60 * 1.1515; $feet = $miles * 5280; $yards = $feet / 3; $kilometers = $miles * 1.609344; $meters = $kilometers * 1000; return compact('miles','feet','yards','kilometers','meters'); } $point1 = array('lat' => 31.2014966,121, 'long' => 121.40233369999998); $point2 = array('lat' => 31.22323799999999, 'long' => 121.44552099999998); $distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); foreach ($distance as $unit => $value) { echo $unit.': '.number_format($value,4).'<br />'; } /** * @desc 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经度值 */ function getDistance3($lat1, $lng1, $lat2, $lng2) { $earthRadius = 6378137; //approximate radius of earth in meters /* Convert these degrees to radians to work with the formula */ $lat1 = ($lat1 * pi() ) / 180; $lng1 = ($lng1 * pi() ) / 180; $lat2 = ($lat2 * pi() ) / 180; $lng2 = ($lng2 * pi() ) / 180; /* Using the Haversine formula http://en.wikipedia.org/wiki/Haversine_formula calculate the distance */ $calcLongitude = $lng2 - $lng1; $calcLatitude = $lat2 - $lat1; $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2); $stepTwo = 2 * asin(min(1, sqrt($stepOne))); $calculatedDistance = $earthRadius * $stepTwo; return round($calculatedDistance); } echo "\n"; echo getDistance3(31.2014966,121.40233369999998,31.22323799999999,121.44552099999998);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 37, Position 2 = 48
Branch analysis from position: 37
2 jumps found. (Code = 78) Position 1 = 38, Position 2 = 48
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
filename:       /in/WbIMr
function name:  (null)
number of ops:  58
compiled vars:  !0 = $point1, !1 = $point2, !2 = $distance, !3 = $value, !4 = $unit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   INIT_FCALL                                               'getdistance'
          1        SEND_VAL                                                 31.2015
          2        SEND_VAL                                                 121.402
          3        SEND_VAL                                                 31.2232
          4        SEND_VAL                                                 121.446
          5        DO_FCALL                                      0  $5      
          6        ECHO                                                     $5
   34     7        ECHO                                                     '%0A'
   35     8        INIT_FCALL                                               'calculatesigndistance'
          9        SEND_VAL                                                 31.2015
         10        SEND_VAL                                                 121.402
         11        SEND_VAL                                                 31.2232
         12        SEND_VAL                                                 121.446
         13        DO_FCALL                                      0  $6      
         14        ECHO                                                     $6
   55    15        ECHO                                                     '%0A'
   56    16        INIT_FCALL                                               'distance'
         17        SEND_VAL                                                 31.2015
         18        SEND_VAL                                                 121.402
         19        SEND_VAL                                                 31.2232
         20        SEND_VAL                                                 121.446
         21        DO_FCALL                                      0  $7      
         22        ECHO                                                     $7
   71    23        ASSIGN                                                   !0, <array>
   72    24        ASSIGN                                                   !1, <array>
   73    25        INIT_FCALL                                               'getdistancebetweenpointsnew'
         26        FETCH_DIM_R                                      ~10     !0, 'lat'
         27        SEND_VAL                                                 ~10
         28        FETCH_DIM_R                                      ~11     !0, 'long'
         29        SEND_VAL                                                 ~11
         30        FETCH_DIM_R                                      ~12     !1, 'lat'
         31        SEND_VAL                                                 ~12
         32        FETCH_DIM_R                                      ~13     !1, 'long'
         33        SEND_VAL                                                 ~13
         34        DO_FCALL                                      0  $14     
         35        ASSIGN                                                   !2, $14
   74    36      > FE_RESET_R                                       $16     !2, ->48
         37    > > FE_FETCH_R                                       ~17     $16, !3, ->48
         38    >   ASSIGN                                                   !4, ~17
   75    39        CONCAT                                           ~19     !4, '%3A+'
         40        INIT_FCALL                                               'number_format'
         41        SEND_VAR                                                 !3
         42        SEND_VAL                                                 4
         43        DO_ICALL                                         $20     
         44        CONCAT                                           ~21     ~19, $20
         45        CONCAT                                           ~22     ~21, '%3Cbr+%2F%3E'
         46        ECHO                                                     ~22
   74    47      > JMP                                                      ->37
         48    >   FE_FREE                                                  $16
  113    49        ECHO                                                     '%0A'
  114    50        INIT_FCALL                                               'getdistance3'
         51        SEND_VAL                                                 31.2015
         52        SEND_VAL                                                 121.402
         53        SEND_VAL                                                 31.2232
         54        SEND_VAL                                                 121.446
         55        DO_FCALL                                      0  $23     
         56        ECHO                                                     $23
         57      > RETURN                                                   1

Function getdistance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WbIMr
function name:  getdistance
number of ops:  61
compiled vars:  !0 = $lng1, !1 = $lat1, !2 = $lng2, !3 = $lat2, !4 = $radLat1, !5 = $radLat2, !6 = $radLng1, !7 = $radLng2, !8 = $a, !9 = $b, !10 = $s
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   12     4        INIT_FCALL                                               'deg2rad'
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $11     
          7        ASSIGN                                                   !4, $11
   13     8        INIT_FCALL                                               'deg2rad'
          9        SEND_VAR                                                 !3
         10        DO_ICALL                                         $13     
         11        ASSIGN                                                   !5, $13
   14    12        INIT_FCALL                                               'deg2rad'
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $15     
         15        ASSIGN                                                   !6, $15
   15    16        INIT_FCALL                                               'deg2rad'
         17        SEND_VAR                                                 !2
         18        DO_ICALL                                         $17     
         19        ASSIGN                                                   !7, $17
   16    20        SUB                                              ~19     !4, !5
         21        ASSIGN                                                   !8, ~19
   17    22        SUB                                              ~21     !6, !7
         23        ASSIGN                                                   !9, ~21
   18    24        INIT_FCALL                                               'asin'
         25        INIT_FCALL                                               'sqrt'
         26        INIT_FCALL                                               'pow'
         27        INIT_FCALL                                               'sin'
         28        DIV                                              ~23     !8, 2
         29        SEND_VAL                                                 ~23
         30        DO_ICALL                                         $24     
         31        SEND_VAR                                                 $24
         32        SEND_VAL                                                 2
         33        DO_ICALL                                         $25     
         34        INIT_FCALL                                               'cos'
         35        SEND_VAR                                                 !4
         36        DO_ICALL                                         $26     
         37        INIT_FCALL                                               'cos'
         38        SEND_VAR                                                 !5
         39        DO_ICALL                                         $27     
         40        MUL                                              ~28     $26, $27
         41        INIT_FCALL                                               'pow'
         42        INIT_FCALL                                               'sin'
         43        DIV                                              ~29     !9, 2
         44        SEND_VAL                                                 ~29
         45        DO_ICALL                                         $30     
         46        SEND_VAR                                                 $30
         47        SEND_VAL                                                 2
         48        DO_ICALL                                         $31     
         49        MUL                                              ~32     $31, ~28
         50        ADD                                              ~33     $25, ~32
         51        SEND_VAL                                                 ~33
         52        DO_ICALL                                         $34     
         53        SEND_VAR                                                 $34
         54        DO_ICALL                                         $35     
         55        MUL                                              ~36     $35, 2
         56        MUL                                              ~37     ~36, 6378.14
         57        MUL                                              ~38     ~37, 1000
         58        ASSIGN                                                   !10, ~38
   19    59      > RETURN                                                   !10
   20    60*     > RETURN                                                   null

End of function getdistance

Function calculatesigndistance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WbIMr
function name:  calculateSignDistance
number of ops:  65
compiled vars:  !0 = $longitude1, !1 = $latitude1, !2 = $longitude2, !3 = $latitude2, !4 = $lat1, !5 = $lng1, !6 = $lat2, !7 = $lng2, !8 = $calcLongitude, !9 = $calcLatitude, !10 = $stepOne, !11 = $stepTwo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   24     4        INIT_FCALL                                               'pi'
          5        DO_ICALL                                         $12     
          6        MUL                                              ~13     !1, $12
          7        DIV                                              ~14     ~13, 180
          8        ASSIGN                                                   !4, ~14
   25     9        INIT_FCALL                                               'pi'
         10        DO_ICALL                                         $16     
         11        MUL                                              ~17     !0, $16
         12        DIV                                              ~18     ~17, 180
         13        ASSIGN                                                   !5, ~18
   26    14        INIT_FCALL                                               'pi'
         15        DO_ICALL                                         $20     
         16        MUL                                              ~21     !2, $20
         17        DIV                                              ~22     ~21, 180
         18        ASSIGN                                                   !6, ~22
   27    19        INIT_FCALL                                               'pi'
         20        DO_ICALL                                         $24     
         21        MUL                                              ~25     !3, $24
         22        DIV                                              ~26     ~25, 180
         23        ASSIGN                                                   !7, ~26
   28    24        SUB                                              ~28     !7, !5
         25        ASSIGN                                                   !8, ~28
   29    26        SUB                                              ~30     !6, !4
         27        ASSIGN                                                   !9, ~30
   30    28        INIT_FCALL                                               'pow'
         29        INIT_FCALL                                               'sin'
         30        DIV                                              ~32     !9, 2
         31        SEND_VAL                                                 ~32
         32        DO_ICALL                                         $33     
         33        SEND_VAR                                                 $33
         34        SEND_VAL                                                 2
         35        DO_ICALL                                         $34     
         36        INIT_FCALL                                               'cos'
         37        SEND_VAR                                                 !4
         38        DO_ICALL                                         $35     
         39        INIT_FCALL                                               'cos'
         40        SEND_VAR                                                 !6
         41        DO_ICALL                                         $36     
         42        MUL                                              ~37     $35, $36
         43        INIT_FCALL                                               'pow'
         44        INIT_FCALL                                               'sin'
         45        DIV                                              ~38     !8, 2
         46        SEND_VAL                                                 ~38
         47        DO_ICALL                                         $39     
         48        SEND_VAR                                                 $39
         49        SEND_VAL                                                 2
         50        DO_ICALL                                         $40     
         51        MUL                                              ~41     $40, ~37
         52        ADD                                              ~42     $34, ~41
         53        ASSIGN                                                   !10, ~42
   31    54        INIT_FCALL                                               'asin'
         55        INIT_FCALL                                               'sqrt'
         56        SEND_VAR                                                 !10
         57        DO_ICALL                                         $44     
         58        SEND_VAR                                                 $44
         59        DO_ICALL                                         $45     
         60        MUL                                              ~46     $45, 2
         61        ASSIGN                                                   !11, ~46
   32    62        MUL                                              ~48     !11, 6.37814e+6
         63      > RETURN                                                   ~48
   33    64*     > RETURN                                                   null

End of function calculatesigndistance

Function distance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WbIMr
function name:  distance
number of ops:  61
compiled vars:  !0 = $lng1, !1 = $lat1, !2 = $lng2, !3 = $lat2, !4 = $radLat1, !5 = $radLat2, !6 = $radLng1, !7 = $radLng2, !8 = $a, !9 = $b, !10 = $s
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   46     4        INIT_FCALL                                               'deg2rad'
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $11     
          7        ASSIGN                                                   !4, $11
   47     8        INIT_FCALL                                               'deg2rad'
          9        SEND_VAR                                                 !3
         10        DO_ICALL                                         $13     
         11        ASSIGN                                                   !5, $13
   48    12        INIT_FCALL                                               'deg2rad'
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $15     
         15        ASSIGN                                                   !6, $15
   49    16        INIT_FCALL                                               'deg2rad'
         17        SEND_VAR                                                 !2
         18        DO_ICALL                                         $17     
         19        ASSIGN                                                   !7, $17
   50    20        SUB                                              ~19     !4, !5
         21        ASSIGN                                                   !8, ~19
   51    22        SUB                                              ~21     !6, !7
         23        ASSIGN                                                   !9, ~21
   52    24        INIT_FCALL                                               'asin'
         25        INIT_FCALL                                               'sqrt'
         26        INIT_FCALL                                               'pow'
         27        INIT_FCALL                                               'sin'
         28        DIV                                              ~23     !8, 2
         29        SEND_VAL                                                 ~23
         30        DO_ICALL                                         $24     
         31        SEND_VAR                                                 $24
         32        SEND_VAL                                                 2
         33        DO_ICALL                                         $25     
         34        INIT_FCALL                                               'cos'
         35        SEND_VAR                                                 !4
         36        DO_ICALL                                         $26     
         37        INIT_FCALL                                               'cos'
         38        SEND_VAR                                                 !5
         39        DO_ICALL                                         $27     
         40        MUL                                              ~28     $26, $27
         41        INIT_FCALL                                               'pow'
         42        INIT_FCALL                                               'sin'
         43        DIV                                              ~29     !9, 2
         44        SEND_VAL                                                 ~29
         45        DO_ICALL                                         $30     
         46        SEND_VAR                                                 $30
         47        SEND_VAL                                                 2
         48        DO_ICALL                                         $31     
         49        MUL                                              ~32     $31, ~28
         50        ADD                                              ~33     $25, ~32
         51        SEND_VAL                                                 ~33
         52        DO_ICALL                                         $34     
         53        SEND_VAR                                                 $34
         54        DO_ICALL                                         $35     
         55        MUL                                              ~36     $35, 2
         56        MUL                                              ~37     ~36, 6378.14
         57        MUL                                              ~38     ~37, 1000
         58        ASSIGN                                                   !10, ~38
   53    59      > RETURN                                                   !10
   54    60*     > RETURN                                                   null

End of function distance

Function getdistancebetweenpointsnew:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WbIMr
function name:  getDistanceBetweenPointsNew
number of ops:  69
compiled vars:  !0 = $latitude1, !1 = $longitude1, !2 = $latitude2, !3 = $longitude2, !4 = $theta, !5 = $miles, !6 = $feet, !7 = $yards, !8 = $kilometers, !9 = $meters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   59     4        SUB                                              ~10     !1, !3
          5        ASSIGN                                                   !4, ~10
   60     6        INIT_FCALL                                               'sin'
          7        INIT_FCALL                                               'deg2rad'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $12     
         10        SEND_VAR                                                 $12
         11        DO_ICALL                                         $13     
         12        INIT_FCALL                                               'sin'
         13        INIT_FCALL                                               'deg2rad'
         14        SEND_VAR                                                 !2
         15        DO_ICALL                                         $14     
         16        SEND_VAR                                                 $14
         17        DO_ICALL                                         $15     
         18        MUL                                              ~16     $13, $15
         19        INIT_FCALL                                               'cos'
         20        INIT_FCALL                                               'deg2rad'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $17     
         23        SEND_VAR                                                 $17
         24        DO_ICALL                                         $18     
         25        INIT_FCALL                                               'cos'
         26        INIT_FCALL                                               'deg2rad'
         27        SEND_VAR                                                 !2
         28        DO_ICALL                                         $19     
         29        SEND_VAR                                                 $19
         30        DO_ICALL                                         $20     
         31        MUL                                              ~21     $18, $20
         32        INIT_FCALL                                               'cos'
         33        INIT_FCALL                                               'deg2rad'
         34        SEND_VAR                                                 !4
         35        DO_ICALL                                         $22     
         36        SEND_VAR                                                 $22
         37        DO_ICALL                                         $23     
         38        MUL                                              ~24     $23, ~21
         39        ADD                                              ~25     ~16, ~24
         40        ASSIGN                                                   !5, ~25
   61    41        INIT_FCALL                                               'acos'
         42        SEND_VAR                                                 !5
         43        DO_ICALL                                         $27     
         44        ASSIGN                                                   !5, $27
   62    45        INIT_FCALL                                               'rad2deg'
         46        SEND_VAR                                                 !5
         47        DO_ICALL                                         $29     
         48        ASSIGN                                                   !5, $29
   63    49        MUL                                              ~31     !5, 60
         50        MUL                                              ~32     ~31, 1.1515
         51        ASSIGN                                                   !5, ~32
   64    52        MUL                                              ~34     !5, 5280
         53        ASSIGN                                                   !6, ~34
   65    54        DIV                                              ~36     !6, 3
         55        ASSIGN                                                   !7, ~36
   66    56        MUL                                              ~38     !5, 1.60934
         57        ASSIGN                                                   !8, ~38
   67    58        MUL                                              ~40     !8, 1000
         59        ASSIGN                                                   !9, ~40
   68    60        INIT_FCALL                                               'compact'
         61        SEND_VAL                                                 'miles'
         62        SEND_VAL                                                 'feet'
         63        SEND_VAL                                                 'yards'
         64        SEND_VAL                                                 'kilometers'
         65        SEND_VAL                                                 'meters'
         66        DO_ICALL                                         $42     
         67      > RETURN                                                   $42
   69    68*     > RETURN                                                   null

End of function getdistancebetweenpointsnew

Function getdistance3:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WbIMr
function name:  getDistance3
number of ops:  74
compiled vars:  !0 = $lat1, !1 = $lng1, !2 = $lat2, !3 = $lng2, !4 = $earthRadius, !5 = $calcLongitude, !6 = $calcLatitude, !7 = $stepOne, !8 = $stepTwo, !9 = $calculatedDistance
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   84     4        ASSIGN                                                   !4, 6378137
   91     5        INIT_FCALL                                               'pi'
          6        DO_ICALL                                         $11     
          7        MUL                                              ~12     !0, $11
          8        DIV                                              ~13     ~12, 180
          9        ASSIGN                                                   !0, ~13
   92    10        INIT_FCALL                                               'pi'
         11        DO_ICALL                                         $15     
         12        MUL                                              ~16     !1, $15
         13        DIV                                              ~17     ~16, 180
         14        ASSIGN                                                   !1, ~17
   94    15        INIT_FCALL                                               'pi'
         16        DO_ICALL                                         $19     
         17        MUL                                              ~20     !2, $19
         18        DIV                                              ~21     ~20, 180
         19        ASSIGN                                                   !2, ~21
   95    20        INIT_FCALL                                               'pi'
         21        DO_ICALL                                         $23     
         22        MUL                                              ~24     !3, $23
         23        DIV                                              ~25     ~24, 180
         24        ASSIGN                                                   !3, ~25
  106    25        SUB                                              ~27     !3, !1
         26        ASSIGN                                                   !5, ~27
  107    27        SUB                                              ~29     !2, !0
         28        ASSIGN                                                   !6, ~29
  108    29        INIT_FCALL                                               'pow'
         30        INIT_FCALL                                               'sin'
         31        DIV                                              ~31     !6, 2
         32        SEND_VAL                                                 ~31
         33        DO_ICALL                                         $32     
         34        SEND_VAR                                                 $32
         35        SEND_VAL                                                 2
         36        DO_ICALL                                         $33     
         37        INIT_FCALL                                               'cos'
         38        SEND_VAR                                                 !0
         39        DO_ICALL                                         $34     
         40        INIT_FCALL                                               'cos'
         41        SEND_VAR                                                 !2
         42        DO_ICALL                                         $35     
         43        MUL                 

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
280.53 ms | 1431 KiB | 41 Q