in the following code, does anyone know why is the $val === 0 case never running when there’s a zero in the $arr array?
<?php
function plusMinus($arr) {
$positiveCount = $zeroCount = $negativeCount = $val = $i = 0;
for( $i; $i < sizeof( $arr ); $i++ ){
$val = (int)$arr[$i];
//echo $val . " <-val - typeofval= " . gettype( $val ) . " n";
if( $val === 0 ){
//print("zero here dude -> " . $val );
}
switch ($val) {
case ($val === 0):
$zeroCount++;
echo "scenario: val=" . $val . " === 0 n";
echo "zero n";
break;
/* case ($val > 0):
$positiveCount++;
echo "scenario: val=" . $val . " > 0 n";
echo "positive n";
break;
case ($val < 0):
$negativeCount++;
echo "scenario: val=" . $val . " < 0 n";
echo "negative n";
break;*/
default:
break;
}
}
}
plusMinus( [0] );
I know this is totally doable with a set of nested if-else statements, BUT, I’d like to understand what’s going on here?
if you uncomment the //print(“zero here dude”); line you’ll see it get in that if, however it never get in the case with the exact same statement, just WHY?
Thanks in advance for taking the time to read.
I tried moving the $val === 0 case to the very top of case list thinking that it might not be running because of the order of the conditions in the switch statement. But that didn’t work.
I also tried removing the integer casting in line $val = (int)$arr[$i]; to see if that would work but no success either.