I have a PHP script that I wrote probably 10 years ago. I don't remember what version PHP was on at the time but my script worked just fine without complaint from the interpreter. Now, I've had to move my scripts to a new web host and, under PHP 7.x, the interpreter complains loudly about a certain line of this script and I'm looking for an elegant way to get it to shut up.
The offending line is:-
list($degrees, $minutes, $seconds) = preg_split("/ /", $coord);
The $coord
variable contains a GPS coordinate in one of three forms: "degrees minutes seconds", "degrees decimal-minutes", or "decimal-degrees". So, the preg_split()
may return 1, 2, or 3 elements. If it returns only 1 or 2 elements, the interpreter complains loudly about the undefined references to $seconds
and/or $minutes
. I see that there is a LIMIT parameter that I could specify for preg_split()
that gives it a maximum number of elements to return but there doesn't seem to be a complimentary parameter to tell it the MINIMUM number of elements to return. Any suggestions welcome.
Sample coords: '-97.74019' or '-97 44.411' or '-97 44 24.7'
Totally agree with Anant, but you can do it in bit more elegant way: