WXforum.net
Miscellaneous Debris => Tech Corner => Topic started by: PaulMy on November 12, 2017, 01:38:46 PM
-
Is there some way to help troubleshoot PHP on a line by line basis to determine where the syntax error may actually be occurring? My PHP knowledge is pretty well next to none :oops:
I am trying out a new script that decodes METAR http://www.komokaweather.com/weather/metar_display.php
After considerable troubleshooting and changes I am now down to this -
Parse error: syntax error, unexpected T_STRING in /home/content/96/5379896/html/weather/decodeMETAR.php on line 158
Line 158 is goto function_exit;
If I rem line 158 by ## then encounter -
Parse error: syntax error, unexpected '[' in /home/content/96/5379896/html/weather/decodeMETAR.php on line 548
Line 548 is $decodeInfo['PLACE'] = find_aerodrome($decodeInfo['STATION'])['name'];
Some of the errors initially encountered were that my hosting version was PHP v5.4.19 and the script was working in v7 by the developer. I have updated my version to v5.6.27 but the script is still not running. I can't upgrade to v7 with GoDaddy until I first change to their cPanel hosting.
Paul
-
For the first error, I'd check line 157 for a semicolon termination.
The second error is an odd use. If the function 'find_aerodrome()' truly returns an keyed array of values, then it might make sense. Otherwise .. the ['name'] at the end is likely the cause of the syntax error.
Maybe replace the line with $t= find_aerodrome($decodeInfo['STATION']);
$decodeInfo['PLACE'] = $t['name'];
which would be a customary format (still assuming that the function returns a keyed array, however)
-
Thanks for comment Ken.
The code before line 158 is
//================================================//
// DIAGNOSTIC FUNCTIONALITY for testing //
//================================================//
# define error handling for development environment, feel free to comment out (or adjust) next line if you use this script in a production environment
error_reporting(E_ALL); // show all errors is requested as although there should not be any errors in this finalised script, highlighting any that do occur rather than hiding them shows confidence!
if (!isset($show_diagnostics))$show_diagnostics = false; // calling script can ask for this script to show its workings for diagnostic purposes, but default is no diagnostics
//================================================//
// Clean up and validate the complete METAR //
//================================================//
$cleaned_metar = decodeMETAR_clean ($raw_metar); // call separate pre-process - found in 'decodeMETAR_sub_funct.php' script file
# Example of minimum length METAR ('Type of Report Group', 'Aerodrome Identity Group', 'Time Group' and 'Identification of an automated or missing report Group'):
# Cranfield Airport 01/07/2017 06:50->
# METAR EGTC 010650Z NIL
if(strlen($cleaned_metar) < 22) // the 22 character definition of shortest valid METAR is a string that starts with "METAR" and ends with "NIL" as in example above
{
$decodeInfo['FEEDBACK'] = "Input string rejected, no decoding"; // FEEDBACK is global element that contains one message, not for actual decoded output
goto function_exit; // 'goto' is allowed in PHP 5.3.0 and higher, and recommended to avoid multiple layers of conditionals and improve readability in PHP 7
}
$decodeInfo['METAR'] = $cleaned_metar; // add whole METAR to output array, so external script can display what has been decoded!
I changed line 548 as per your suggestion and then it progressed to other lines with errors which I changed from colon : to semi-colon ; and then the decode_METAR.php completed and decode_METAR_sub_funct.php started and got an error in it in line 133
Parse error: syntax error, unexpected '[' in /home/content/96/5379896/html/weather/decodeMETAR_sub_funct.php on line 133
Line 133 is $colour_array = ['', 'BLU+', 'BLU', 'WHT', 'GRN', 'YLO1', 'YLO2', 'AMB', 'RED'];
The lines before 133 seem ok to me
$miscellaneousTypes = array(
'Dust devil' => 'PO',
'Squalls' => 'SQ',
'Funnel Cloud' => 'FC', // Tornado and Waterspout denoted by + in intensity
'Dust Storm' => 'DS',
'Sand Storm' => 'SS'
);
// Initialisations for Colour State codes are next, as that snippet of script can be entered more than once
# Example from Meierwik, Glücksburg, Germany: METAR ETGG 011220Z 18011kt 9999 FEW030 17/10 Q1019 BLU+
# Version 0.2.0 of this script did not decode above METAR due to '+' after colour code, so in version 1.0.0 have added just that one,
# assuming only best can be modified!
$colour_array = ['', 'BLU+', 'BLU', 'WHT', 'GRN', 'YLO1', 'YLO2', 'AMB', 'RED'];
/////////////////////////////////////////////////
so the progress stalls for me ](*,) Fortunately someone with a lot more knowledge is having a look at it.
Paul
-
Line 133 should read as
$colour_array = array('', 'BLU+', 'BLU', 'WHT', 'GRN', 'YLO1', 'YLO2', 'AMB', 'RED');
The syntax using [] works for JavaScript, but not for PHP (where an explicit array() has to be used) :)
-
Thanks so much Ken, that change has fixed this issue. And I'll go back into the scripts because I think some similar issue was there.
One more question if I may...
After this line 133 fix I now get
Parse error: syntax error, unexpected '[' in /home/content/96/5379896/html/weather/decodeMETAR_sub_funct.php on line 445
Line 133 is $beaufort_force = calculateBeaufort($output['knots'],'kts')[1];
and the code around there is
// ------------------------------------------------
# these two lines are addition by SPAWS to select drawing
# IMPORTANT - If you don't have the external script 'forbidden\sourceView.php', you won't have function 'calculateBeaufort'
if(function_exists('calculateBeaufort')) {
$beaufort_force = calculateBeaufort($output['knots'],'kts')[1];
# IMPORTANT - The next image set is specific to the SPAWS web site design, remove this snippet of code if using this script elsewhere
if(file_exists("../gauges_images/beaufort_" . $beaufort_force . ".png")) $output['house'] = "../gauges_images/beaufort_" . $beaufort_force . ".png";
}
//-------------------------------------------------
Ok, a second question. Are some of the errors I noted PHP version specific - i.e. could they have worked in PHP v7?
Regards,
Paul
-
That
$beaufort_force = calculateBeaufort($output['knots'],'kts')[1];
is odd. Like I'd cited earlier, using [] after a function call is unusual. If the function returns an array, then the proper way is to $t = calculateBeaufort($output['knots'],'kts');
$beaufort_force = $t[1];
It looks like the author of the code was trying to use JavaScript syntax a bit with PHP, and that doesn't always work.
-
Hi Ken,
With the guidance of a more knowledgeable hobbyist/forum member I now have the script running
I can now give feedback to the author.
The problem in getting the script to run was primarily that while I am on (or suppose to be on) PHP v 5.6 it seems my GoDaddy hosting is using 2 versions of PHP. In the root and most sub folders like www.komokaweather.ca (http://www.komokaweather.ca) and www.komokaweather.com/weather28 (http://www.komokaweather.com/weather28) and others it is using v 5.6.27. However in the www.komokaweather.com/weather (http://www.komokaweather.com/weather) sub folder where I was running the metar_display.php it is using v 5.2.17 and that caused many problems within the script. I have now moved the script to its own sub folder /metar and that is using v 5.6.27.
Contacting GoDaddy is on the schedule...
Thanks Ken and Brian.
Paul