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
}
通常,会观察到多个云层,因此,在遍历$cloudCoverage
的同时,层中的每个$metarClouds[0]
都会添加到另一个上。但是,如果没有云层(清除或“CLR”),那么它应该注册为0.它确实如此。但是,代码以某种方式返回“Overcast”。
我检查了两个switch语句,以确保在我预期的时候传递“CLR”,并且$cloudCoverage
在我预期时等于零。每次都是这样。并且,$skyCoverage
在返回之前仍然注册为零。
我已经尝试将$cloudCoverage
设置为1-1,0 / 1,0,0或其他一些方法,以确保PHP不会以某种方式将其视为null并且...某种方式...错误地处理它。如果我重写$cloudCoverage = 0.01
并通过第一个开关传递CLR,则所有内容都正确通过第二个开关:清除。我也尝试将最后一个案例设置为case 0:
等,但仍然有相同的错误结果。
答案 0 :(得分:2)
让我们仔细看看
switch ( $skyCoverage ) {
case ( $skyCoverage >= 1.00 ):
评估。首先让我们用0替换$ skyCover
switch ( 0 ) {
case ( 0 >= 1.00 ):
现在评估案例条件
switch ( 0 ) {
case ( false ):
因为(bool)0 === false,这是一场比赛
(见http://docs.php.net/language.types.boolean#language.types.boolean.casting)
正如pygorex1所说,这不是switch/case
的工作方式。你想要一个
if (...) {
}
else if (...) {
}
else {
}
代替。
(编辑:使用“相同的运算符”===在“(bool)0 === false”中不是一个好的解释.titch / case执行loose comparison,即它允许{{3 ,如在0 == false,其计算结果为TRUE)
答案 1 :(得分:1)
这不是switch
语句的工作方式:
http://us.php.net/manual/en/control-structures.switch.php
编辑:请参阅此代码段:
switch ( $skyCoverage ) {
case ( $skyCoverage >= 1.00 ):
$skyCondition = "Overcast";
当$skyCoverage
为0
时,语句$skyCoverage >= 1.00
将评估为false。因此,case ( $skyCoverage >= 1.00 )
语句的评估方式如下:case ( false )
自0 == false
后,最终结果将为Overcast
答案 2 :(得分:0)
我认为问题在于:
( $skyCoverage >= 1.00 )
计算结果为false或0,当您将其与$ skyCoverage(0)进行比较时,会自动选择第一个。