Tracking Previously Declared

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Tracking Previously Declared

Post by SpecialK »

I am getting a weird error when using a metar code. Found at http://www.trebax.dk/php/php/phpweather/demo_udv.php

This code has been used on another site with no problems. It's slightly modified to how we want to use it.

There are no problems with the includes as no errors happen, but when I try to call a function, is when the error is thrown.

Process just puts the metar string into an associated array by the expected items.

Code: Select all

 
  $metar = get_metar($station,$wxInfo);
 
//process_metar($metar,$wxInfo);
 
Once I uncomment the function call, it breaks with the following error
PHP Fatal error: Cannot redeclare speed() (previously declared in /xxx/metar_inc.php:102) in /xxx/metar_inc.php on line 102, referer: http://thewebsite

I haven't used functions within functions previously, so I'm wondering if it might be doing something dynamic. I did a search for the speed() function but it only exists in the one file.

Code: Select all

 
function process_metar($metar, &$wxInfo) {
    //   This function directs the examination of each group of the METAR. The problem
    // with a METAR is that not all the groups have to be there. Some groups could be
    // missing. Fortunately, the groups must be in a specific order. (This function
    // also assumes that a METAR is well-formed, that is, no typographical mistakes.)
    //   This function uses a function variable to organize the sequence in which to
    // decode each group. Each function checks to see if it can decode the current
    // METAR part. If not, then the group pointer is advanced for the next function
    // to try. If yes, the function decodes that part of the METAR and advances the
    // METAR pointer and group pointer. (If the function can be called again to
    // decode similar information, then the group pointer does not get advanced.)
    if ($metar != '') {
        $metarParts = explode(' ',$metar);
        $groupName = array('get_station','get_time','get_station_type','get_wind','get_var_wind','get_visibility','get_runway','get_conditions','get_cloud_cover','get_temperature','get_altimeter');
        $metarPtr = 1;  // get_station identity is ignored
        $group = 1;
 
        while ($group < count($groupName)) {
            $part = $metarParts[$metarPtr];
            $groupName[$group]($part,$metarPtr,$group,$wxInfo);  // $groupName is a function variable
            }
        }
    else $wxInfo['ERROR'] = 'Data not available';
    }
function get_wind($part, &$metarPtr, &$group, &$wxInfo) {
    // Decodes wind direction and speed information.
    // Format is dddssKT where ddd = degrees from North, ss = speed, KT for knots,
    // or dddssGggKT where G stands for gust and gg = gust speed. (ss or gg can be a 3-digit number.)
    // KT can be replaced with MPH for meters per second or KMH for kilometers per hour. 
 
    function speed($part, $unit) {
        // Convert wind speed into miles per hour.
        // Some other common conversion factors (to 6 significant digits):
        //   1 mi/hr = 1.15080 knots  = 0.621371 km/hr = 2.23694 m/s
        //   1 ft/s  = 1.68781 knots  = 0.911344 km/hr = 3.28084 m/s
        //   1 knot  = 0.539957 km/hr = 1.94384 m/s
        //   1 km/hr = 1.852 knots    = 3.6 m/s
        //   1 m/s   = 0.514444 knots = 0.277778 km/s
        if ($unit == 'KT') $speed = round(1.1508 * $part);         // from knots
        elseif ($unit == 'MPS') $speed = round(2.23694 * $part);   // from meters per second
        else $speed = round(0.621371 * $part);                     // from km per hour
        $speed = "$speed mph";
        return $speed;
        }
    
    if (ereg('^([0-9G]{5,10}|VRB[0-9]{2,3})(KT|MPS|KMH)$',$part,$pieces)) {
        $part = $pieces[1];
        $unit = $pieces[2];
        if ($part == '00000') {
            $wxInfo['WIND'] = 'calm';  // no wind
            }
        else {
            ereg('([0-9]{3}|VRB)([0-9]{2,3})G?([0-9]{2,3})?',$part,$pieces);
            if ($pieces[1] == 'VRB') $direction = 'varies';
            else {
                $angle = (integer) $pieces[1];
                $compass = array('N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW');
                $direction = $compass[round($angle / 22.5) % 16];
                }
            if ($pieces[3] == 0) $gust = '';
            else $gust = ', gusting to ' . speed($pieces[3], $unit);
            $wxInfo['WIND'] = $direction . ' at ' . speed($pieces[2], $unit) . $gust;
            }
        $metarPtr++;
        }
    $group++;
    }
 
 
Any thoughts on what could be redeclaring speed() on the function call. Is there a way to better track where it is being re-declared?
Post Reply