PHP切换麻烦[关闭]

When U are sleepy and look at PHP switches after Ruby switches..

UPDATE 3 (answer)

See problem below. Its my mistake. All right:) . In PHP. With his dynamic typecasting ( false->(int)false->0: here I getting 0 equals to my'>=17_<=20') and implict behaviour of construct switch in this specific case ( switch( ZERO ) { case (true) break; case (false) break;} ). This behaviour looks strange from side of strong typed languages. Thanks for all!!

SO,please,help! I noticed a strange behavior of "checked" checkboxes on one page (choosing age groups). In database, if user checked <16 - field age equals 0 (zero). And I began to see the logic for "checking". Found next:

   $item = 0; //!!!!!!!
   switch($item){
       case ($item<=16):
         $case = '<16';
         break;
       case ( ($item>=17) && ($item<=20)):
         $case = '>=17_<=20';
         break;
       case ( ($item>=21) && ($item<=25)):
         $case = '>=21_<=25';
         break;
   }
   echo $item.' '.$case.'<br/>';

OUTPUT

 0 >=17_<=20 // HOW???!!!!!!!!!!!!!!!!

TEST

  $range = range(-5,5);

  foreach($range as $item){

      switch($item){
          case ($item<=16):
                  $case = '<16';
                  break;
          case ( ($item>=17) && ($item<=20)):
                  $case = '>=17_<=20';
                  break;
          case ( ($item>=21) && ($item<=25)):
                  $case = '>=21_<=25';
                  break;
         }

         echo $item.' '.$case.'<br/>';
     }

// OUTPUT

 -5 <16
 -4 <16
 -3 <16
 -2 <16
 -1 <16
 0 >=17_<=20 // WHY???????!!!!!!!!!!!!!!
 1 <16
 2 <16
 3 <16
 4 <16
 5 <16

http://3v4l.org/jbfng Please,explain me, WHAT IS THIS?!

UPDATE 1 No,MEN, I am not crazy! All MUST works. Loop only for demonstration of this strange behavior. If 0 (zero) < 16, $case MUST be equals <16. NO????????

UPDATE 2 Honestly, I don`t understand minuses. Ok.

    $item = 0;
    if (($item>=17) && ($item<=20)){ // false
        // never for  <17 & >20
    }

    item = 0;
    $checking  = (($item>=17) && ($item<=20)); // false
    if ($checkig){
        // never for  <17 & >20
    }

    item = 0;
    $checkig = (($item>=17) && ($item<=20)) ? true : false; // false
    if ($checkig){
         // never for  <17 & >20
    }      

    $item = 0;
    switch($item){
       case ( ($item>=17) && ($item<=20)): // false
          // never for  <17 & >20
          break;
    }

All works fine. Its only when EQUALS ZERO. Or You want to say,that OUTPUT(Test2) - its normal behaviour??? Thanks.

TEST 2

      $range = range(-5,47);
           //var_dump($range);

            foreach($range as $item){
                  switch($item){
                      case ($item<=16):
                            $case = '<16';
                            break;
                      case ( ($item>=17) && ($item<=20)):
                            $case = '>=17_<=20';
                            break;
                      case ( ($item>=21) && ($item<=25)):
                            $case = '>=21_<=25';
                            break;
                      case ( ($item>=26) && ($item<=35)):
                            $case = '>=26_<=35';
                            break;
                      case ( ($item>=36) && ($item<=45)):
                             $case = '>=36_<=45';
                             break;
                       case ( ($item>=46)):
                             $case = '>=46';
                             break;
                   }

                    echo $item.' '.$case.'<br/>';
           }

// OUTPUT

-5 <16
-4 <16
-3 <16
-2 <16
-1 <16
0 >=17_<=20 // WHY ?
1 <16
2 <16
3 <16
4 <16
5 <16
6 <16
7 <16
8 <16
9 <16
10 <16
11 <16
12 <16
13 <16
14 <16
15 <16
16 <16
17 >=17_<=20
18 >=17_<=20
19 >=17_<=20
20 >=17_<=20
21 >=21_<=25
22 >=21_<=25
23 >=21_<=25
24 >=21_<=25
25 >=21_<=25
26 >=26_<=35
27 >=26_<=35
28 >=26_<=35
29 >=26_<=35
30 >=26_<=35
31 >=26_<=35
32 >=26_<=35
33 >=26_<=35
34 >=26_<=35
35 >=26_<=35
36 >=36_<=45
37 >=36_<=45
38 >=36_<=45
39 >=36_<=45
40 >=36_<=45
41 >=36_<=45
42 >=36_<=45
43 >=36_<=45
44 >=36_<=45
45 >=36_<=45
46 >=46
47 >=46

the expression ($item>=17) && ($item<=20) evaluates to 0, as the value of $item is 0. the switch-case statement will merely match the value of $item with case values. So, your code, in first case is equivalent to

switch($item){
       case (1):
         $case = '<16';
         break;
       case ( 0):
         $case = '>=17_<=20';
         break;
       case ( 0):
         $case = '>=21_<=25';
         break;
 }

You have the error since "0" evaluates to false. Try this:

<?
  echo 0==(0>=17) && (0<=20)); //true
?>

This is because right side of the == evaluates to false and 0==false is true so you hit that condition in your switch statement.

Switch statement compares the condition with the switched variable (in your case item) so it evaluates to true, and it enter the switch condition and breaks it there.

You probably shouldn't be using a switch here. Use this:

$range = range(-5,5);

foreach($range as $item){
    if ($item<=16)
        $case = '<16';
    else if ( ($item>=17) && ($item<=20))
        $case = '>=17_<=20';
    else if( ($item>=21) && ($item<=25))
        $case = '>=21_<=25';


     echo $item.' '.$case.'<br/>';
 }