Hi guys I am having problems with this switch statement:

<?php

$number = 1;


switch( $number ){

case ($number <= 10):
echo "tiny number";
break;


case ($number > 10 && <= 50):
echo "medium number";
break;


default:
echo "large number";

}

?>

For some reason I keep getting an error message for the following snippet:

case ($number > 10 && <= 50):

... I don't know where I am going wrong. If I delete:

case ($number > 10 && <= 50):
echo "medium number";
break;

...... it works just fine. But obviously I still want to include this snippet so it functions the way I want it to.

Paul.

    Like this

    <?php 
    
    $number = 1; 
    
    switch($number)
    	{ 
    		case ($number <= 10): 
    		echo "tiny number"; 
    		break; 
    
    	case ($number > 10 && $number <= 50): 
    	echo "medium number"; 
    	break; 
    
    	default: 
    	echo "large number"; 
    } 
    
    ?>
    
    

    Don't forget to add the variable you're testing

      Ahh. My bad! Thanks man.

      Funny how Ive been coding php for a few years now and I still over look these basic things!

        Paul help!;11062127 wrote:

        Ahh. My bad! Thanks man.

        Funny how Ive been coding php for a few years now and I still over look these basic things!

        As every single person on this forum as once told me in one way or the other:

        Indentation is important

          It's actually wrong.

          The switch statement takes the value in [font=monospace]switch($value)[/font] and then steps through each of the cases [font=monospace]case $case[/font],
          testing [font=monospace]$value==$case[/font] each time, until one of those tests evaluates to [font=monospace]true[/font].

          It's for cleaner code when you have a bunch of [font=monospace]if($value=='foo'){}elseif($value=='bar'){}elseif($value=='baz'){}...[/font] so you don't have to keep writing [font=monospace]$value==[/font] all the time.

          What you have is equivalent to

          if($number == ($number <= 10))
          {
          	echo "tiny number";
          }
          elseif($number == ($number > 10 && $number <= 50))
          {
          	echo "medium number";
          }
          else
          {
          	echo "large number";
          }

          When instead you want

          if($number <= 10)
          {
          	echo "tiny number";
          }
          elseif($number > 10 && $number <= 50)
          {
          	echo "medium number";
          }
          else
          {
          	echo "large number";
          }

            I'm curious, when would be the best time to actually use switch? I'm guessing both myself and Paul might benefit from that

              When you have a value and you wish it to compare it against a list of other values, and do something based on which one matches.

              Or, as the manual puts it on its page for the [man]switch[/man] statement:

              In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

                cluelessPHP;11062133 wrote:

                I'm curious, when would be the best time to actually use switch? I'm guessing both myself and Paul might benefit from that

                As weed mentioned, with you want to check for specific values each individually, such as in my parameter class, to return the PDO equivalent type:

                    public function GetType()
                    {
                        switch ($this->Type) {
                            case DB_PARAM_INT:
                                return \PDO::PARAM_INT;
                            case DB_PARAM_BOOL:
                                return \PDO::PARAM_BOOL;
                            case DB_PARAM_NULL:
                                return \PDO::PARAM_NULL;
                            case DB_PARAM_STR:
                            case DB_PARAM_DEC:
                                return \PDO::PARAM_STR;
                            default:
                                throw new \UnexpectedValueException('Unexpected value found in Parameter::Type for PDO::MySQL');
                        }
                    }

                This is functionally equivalent to typing:

                if ($this->Type == DB_PARAM_INT)
                    return \PDO::PARAM_INT;
                elseif ($this->Type == DB_PARAM_BOOL)
                    return \PDO::PARAM_BOOL;
                // etc..
                

                  There is a way you can use a switch() to do that, though I won't claim it's considered the best way (nor will I claim it isn't):

                  <?php
                  
                  $number = rand(-10, 100);
                  
                  switch(true)
                  {
                      case ($number <= 10):
                          $result = "tiny";
                          break;
                  
                  case ($number > 10 && $number <= 50):
                      $result = "medium";
                      break;
                  
                  default:
                      $result = "large";
                  }
                  echo "$number is a $result number".PHP_EOL;
                  
                  [~]$ php switch.php
                  99 is a large number
                  [~]$ php switch.php
                  14 is a medium number
                  [~]$ php switch.php
                  7 is a tiny number
                  

                    Though by then the only advantage it has over a series of if/elseif statements is if some of the cases involve falling through to others. (Interestingly, mention of that form has been dropped from the manual's description; possibly because it caused confusion.)

                      Weedpacket;11062143 wrote:

                      Interestingly, mention of that form has been dropped from the manual's description; possibly because it caused confusion.

                      Nah, it's still there

                      php.net wrote:

                      PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example:

                      <?php
                      switch ($i) {
                          case 0:
                              echo "i equals 0";
                          case 1:
                              echo "i equals 1";
                          case 2:
                              echo "i equals 2";
                      }
                      ?>
                      

                      Here, if $i is equal to 0, PHP would execute all of the echo statements! If $i is equal to 1, PHP would execute the last two echo statements. You would get the expected behavior ('i equals 2' would be displayed) only if $i is equal to 2. Thus, it is important not to forget break statements (even though you may want to avoid supplying them on purpose under certain circumstances).

                        Sorry, by "that form" I meant the [font=monospace]if(true){ case $expr:....}[/font] that NogDog wrote of. That might as well be a chain of if/else statements, fallthrough notwithstanding.

                          Ah gotcha. I thought you meant it dropped the fall through stuff. Honestly, never though the switch(CONSTANT_EXPRESSION_HERE) form made any sense.

                            Just to make the switch() look better:

                            $result = $number <= 10 ? 'tiny' : ($number <= 50 ? 'medium' : 'large');
                            

                            😉

                              NogDog;11062159 wrote:

                              Just to make the switch() look better:

                              $result = $number <= 10 ? 'tiny' : ($number <= 50 ? 'medium' : 'large');
                              

                              😉

                              Thanks ... now, please pass the ibuprofen? :p

                                dalecosp;11062169 wrote:

                                Thanks ... now, please pass the ibuprofen? :p

                                It's not so bad, even I could read that one :p

                                  cluelessPHP;11062173 wrote:

                                  It's not so bad, even I could read that one :p

                                  Well yeah, I can read it; I can even tell what it does ... nesting ternary assignments just makes my brain hurt. 😃

                                  I might also injure myself typing that if I've not cut my fingernails recently ... (fortunately that's not usually an issue ...)

                                    If I have that and there's a need for it to stay that way, I at least reformat it:

                                    $result = $number <= 10 ? 'tiny'   : (
                                              $number <= 50 ? 'medium' :
                                                              'large'
                                              );
                                      <?php $p=function($a,$b){return $a<=$b;};$n=rand(-10,100);?>
                                      <p><?= $n ?> is a <?= $p($n,20)?'small':($p($n,50)?'medium':'large');?> number</p>
                                      

                                      :evilgrin:

                                        NogDog;11062183 wrote:
                                        <?php $p=function($a,$b){return $a<=$b;};$n=rand(-10,100);?>
                                        <p><?= $n ?> is a <?= $p($n,20)?'small':($p($n,50)?'medium':'large');?> number</p>
                                        

                                        :evilgrin:

                                        Bad dog!