PHP中无法识别变量


Variable not recognised in PHP

我正在尝试使用这里提供的phpapi:https://forums.digitalpoint.com/threads/uk-post-code-distance-calculator-using-php-mysql-3-super-easy-steps.1823109/.代码中有一些问题,我不得不修复,但我不知道为什么会出现这个错误。

这是config.php文件:

        <?php
        include('/config.php');
        /*
         * Created on May 20, 2010
         *
         * Programmed by Manoj Kumar
         */
        //Connect To Database
        $host_sql = $host;
        $username_sql = $username;
        $password_sql = $password;
        $dbname_sql = $dbname;

        $con = mysqli_connect($host_sql, $username_sql, $password_sql, $dbname_sql);
        ?>

这是distCalc.php文件

        <?php
        /*
         * Created on Jun 1, 2010
         *
         * To change the template for this generated file go to
         * Window - Preferences - PHPeclipse - PHP - Code Templates
         */
         include('/api/postcodedistance/config.php');

         //distCalc("NW4 4TE","ZE3 8HB");
         function distCalc($pc1_full,$pc2_full) {

         #Convert the post code to upper case and trim the variable
        $pc1_full = strtoupper(trim($pc1_full));
        #Remove any spaces
        $pc1_full = str_replace(" ","",$pc1_full);
        #Trim the last 3 characters off the end
        $pc1 = substr($pc1_full,0,strlen($pc1_full)-3);
        #Convert the post code to upper case and trim the variable
        $pc2_full = strtoupper(trim($pc2_full));
        #Remove any spaces
        $pc2_full = str_replace(" ","",$pc2_full);
        #Trim the last 3 characters off the end
        $pc2 = substr($pc2_full,0,strlen($pc2_full)-3);

         $sql="SELECT * FROM `hwz_postcodes` WHERE `outcode` = '$pc1'";
         $result=mysqli_query($con, $sql);
         $row=mysqli_fetch_array($result);
         $pc1_lat=$row['latitude'];
         $pc1_long=$row['longitude'];

         $sql="SELECT * FROM `hwz_postcodes` WHERE `outcode` = '$pc2'";
         $result=mysqli_query($con, $sql);
         $row=mysqli_fetch_array($result);
         $pc2_lat=$row['latitude'];
         $pc2_long=$row['longitude'];
         //echo "<br/>".$pc1."<br/>  ".$pc1_lat."<br/>  ".$pc1_long;
         //echo "<br/>".$pc2."<br/>  ".$pc2_lat."<br/>  ".$pc2_long;
         $distance = getDistance($pc1_lat, $pc1_long, $pc2_lat, $pc2_long);
        //echo "<br/>Distance:".$distance;
        return $distance;
         }

         function getDistance($lat1, $long1, $lat2, $long2){
        #$earth = 6371; #km change accordingly
        $earth = 3960; #miles
        #Point 1 cords
        $lat1 = deg2rad($lat1);
        $long1= deg2rad($long1);
        #Point 2 cords
        $lat2 = deg2rad($lat2);
        $long2= deg2rad($long2);
        #Haversine Formula
        $dlong=$long2-$long1;
        $dlat=$lat2-$lat1;
        $sinlat=sin($dlat/2);
        $sinlong=sin($dlong/2);
        $a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong);
        $c=2*asin(min(1,sqrt($a)));
        $d=round($earth*$c);
        return $d;
        }

        ?>

这是我得到的错误:

Notice: Undefined variable: con in C:'Users'Kabeer'Dropbox'Projects'project'api'postcodedistance'distCalc.php on line 38

Con是被定义的,所以我很困惑问题是什么。如果我把Con设为全局的,那么我会得到一个解析错误。我是这样做的:

global $con = mysqli_connect($host_sql, $username_sql, $password_sql, $dbname_sql);

非常感谢

Kabeer

在函数中也添加全局关键字:

function distCalc($pc1_full,$pc2_full) {
    global $con;
    //...
}

在你创建连接之前:

global $con;
$con = mysqli_connect($host_sql, $username_sql, $password_sql, $dbname_sql);

只需将global $con放入函数中即可。