Jump to content
Larry Ullman's Book Forums

jimlongo

Members
  • Posts

    3
  • Joined

  • Last visited

Posts posted by jimlongo

  1. Thanks HartleySan. When you say php.net has a decent description are you referring to the following . . .

    Note that switch/case does loose comparision.

    The construct switch(true) is a little beyond the single Chapter 6 description we've been introduced to so far.

     

    Back to the question of syntax, does it make more sense to compare strings than numbers with switch?

     

    I feel like I've descended down a rabbit hole, but I'm sure it will all make sense eventually, for instance (and I guess this is confirming what you wrote)

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

    returns "i equals 0" . . . however

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

    returns "i equals 1"

  2. Hi, I'm struggling with this particular use of a switch statement.

    I'm trying to use a case that is a range of numbers.

    Seems to work alright, until you change the variable $number to 0 (ZERO).

     

    <?php
    $number=3;
    switch ($number) {
    case $number >=0 && $number <=10:
     print "The number is between 0 and 10";
     break;
    
     case $number >=11 && $number <=20:
     print "The number is between 11 and 20";
     break;
    
     default:
     print "Your number is not between 0 and 20";
    }
    
    ?>
    

     

    Or many other forms I've tried.

     

    case >=0 && <=10:

     

    case ">=0 && <=10":

     

    case ">=0" && "<=10":

     

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

     

    case ($number >="0" && $number <="10"):

     

     

    I guess the longer question is what is the correct way to write this idea so that it works?

    I've looked at it so long I'm probably missing something real simple (like how to deal with zero as an integer), but I can't see it and any help would be appreciated greatly.

     

    Many Thanks,

    love the book!

×
×
  • Create New...