views:

44

answers:

3

function skyCoverage( $metarClouds ) {

    foreach( $metarClouds[0] as $cloudReport ) {
        $coverageCode = substr( $cloudReport, 0, 3 );

        // I check $coverageCode here, and it is indeed "CLR"

            switch( $coverageCode ) {
                case "CLR":
                    $cloudCoverage = 0;
                    break;
                case "FEW":
                    $cloudCoverage = 1/8;
                    break;
                case "SCT":
                    $cloudCoverage = 3/8;
                    break;
                case "BKN":
                    $cloudCoverage = 5/8;
                    break;
                case "OVC":
                    $cloudCoverage = 8/8;
                    break;
            }
    $skyCoverage = $skyCoverage + $cloudCoverage;
    }

// I check $skyCoverage here, and it is indeed 0

    switch ( $skyCoverage ) {
        case ( $skyCoverage >= 1.00 ):
                $skyCondition = "Overcast";
                // I do an echo $skyCoverage; here, and it actually spits out 0 still, even though it obviously shouldn't do anything at all
                break;
        case ( $skyCoverage >= 0.75 ):
                $skyCondition = "Cloudy";
                break;
        case ( $skyCoverage >= 0.625 ):
                $skyCondition = "Mostly Cloudy";
                break;
        case ( $skyCoverage >= 0.5 ):
                $skyCondition = "Scattered Clouds";
                break;
        case ( $skyCoverage >= 0.375 ):
                $skyCondition = "Partly Cloudy";
                break;
        case ( $skyCoverage >= 0.125 ):
                $skyCondition = "Mostly Clear";
                break;
        case ( $skyCoverage < 0.125 ):
                $skyCondition = "Clear";
                break;
    }

// $skyCoverage is still zero here

return $skyCondition;
// Somehow $skyCondition is "Overcast" with $skyCoverage = 0

}

Typically, more than one cloud layer is observed and, therefore, each $cloudCoverage at the layer is added onto the other while looping through $metarClouds[0]. However, if there is no cloud layer (clear, or "CLR"), then it should register as 0. And it does. However, the code somehow returns "Overcast".

I've checked both switch statements to ensure that "CLR" is being passed when I expect it is, and that $cloudCoverage is equating to zero when I expect. It is, every time. And, $skyCoverage still registers as zero just before the return.

I have tried setting $cloudCoverage to 1-1, 0/1, 0.0, or some other method to ensure that PHP isn't somehow treating it as null and ... somehow ... processing it incorrectly. If I rewrite so that $cloudCoverage = 0.01 and pass CLR through the first switch, everything comes through the second switch correctly: Clear. I've also tried setting the last case to case 0: etc., but still have the same erroneous result.

+1  A: 

That's not how switch statements work:

http://us.php.net/manual/en/control-structures.switch.php

Edit: take this code snippet:

switch ( $skyCoverage ) {
    case ( $skyCoverage >= 1.00 ):
            $skyCondition = "Overcast";

When $skyCoverage is 0 the statement $skyCoverage >= 1.00 will evaluate to false. So, the case ( $skyCoverage >= 1.00 ) statement is evaluated as this: case ( false ) and since 0 == false the end result will be Overcast

pygorex1
+2  A: 

Let's take a closer look at how

switch ( $skyCoverage ) {
  case ( $skyCoverage >= 1.00 ):

is evaluated. First let's substitute $skyCover by 0

switch ( 0 ) {
  case ( 0 >= 1.00 ):

Now evaluate the case-condition

switch ( 0 ) {
  case ( false ):

And since (bool)0===false, it's a match
(see http://docs.php.net/language.types.boolean#language.types.boolean.casting).
As pygorex1 said that's not the way switch/case works. You want an

if (...) {
}
else if (...) {
}
else {
}

instead.

(edit: using the "identical operator" === in "(bool)0===false" isn't a good explanation. switch/case performs a loose comparison, i.e it allows type juggling, like in 0==false which evaluates to TRUE)

VolkerK
A: 

I think the problem is that:

( $skyCoverage >= 1.00 )

evaluates to false or 0 and as you're comparing it with $skyCoverage (0), the first one is automatically selected.

jeroen