i found the rationale, the reasoning. maybe others might want to take note.
interestingly , if an integer is fed into the switch codes i.e. $event=0, the switch function
evaluates to true ( it evaluates to int 0 which is true and because of the 'loose' type comparison of the switches as mentioned in ealier post ). thus it enters the cases.
it then peforms the case comparison, but could not find a $event = 0 instance ( i.e. case 0: ).
so it then does nothing. the default is not executed, and there is no true case so there is effectively no statement to execute.
this i found out by breaking up the switch into individual
if's and re-evaluating its behavior. the results are laid out below.
if this is what is said to be documented and intended behavior, so be it.
but certainly to me, such behaviour is wrong. upon not finding a match, the default must be executed. programs must do this, because we rely on defaults, regardless of input.
at the very least, a warning produced to indicate an input type mismatch. this will allows us to handle uncertain inputs. of course it is easily solved by type casting as i mentioned, but just having to do that means lesser flexibility of the switches, and take more energy form the coder to focus on langauge, instead of the intended functionality.
Code: Select all
$event = 0;
echo test($event);
$event = (string) 0;
echo test($event);
$event = 'abort';
echo test($event);
function test($event)
{
echo '<hr />';
echo '$event = '; var_dump($event);
echo '<hr />';
echo '$event is of type '.gettype($event);
echo '<br />';
echo 'comparison is $event==\'abort\' : result => ';
if ($event=='abort') echo gettype($event).' is equal to '.$event; else echo gettype($event).' is NOT equal to (DEFAULT) '.$event;
echo '<br />';
echo 'comparison is $event===\'abort\' : result => ';
if ($event==='abort') echo gettype($event).' is identical to '.$event; else echo gettype($event).' is NOT identical to (DEFAULT) '.$event;
echo '<br />EOT<br />';
}
die();
Results
Code: Select all
$event = int(0) $event is of type integer
comparison is $event=='abort' : result => integer is equal to 0
comparison is $event==='abort' : result => integer is NOT identical to (DEFAULT) 0
EOT
$event = string(1) "0" $event is of type string
comparison is $event=='abort' : result => string is NOT equal to (DEFAULT) 0
comparison is $event==='abort' : result => string is NOT identical to (DEFAULT) 0
EOT
$event = string(5) "abort" $event is of type string
comparison is $event=='abort' : result => string is equal to abort
comparison is $event==='abort' : result => string is identical to abort
EOT