Switch statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcel

    Switch statement

    <?php

    switch ($var) {

    case ("02100" || "02700" || "02500"):

    /*
    rest of code......
    */

    break;

    }

    ?>

    Does this work???? (i mean the || statement(s) in combination with a switch
    statement?

    Marcel


  • Luke Ross

    #2
    Re: Switch statement

    Marcel wrote:[color=blue]
    > <?php
    >
    > switch ($var) {
    >
    > case ("02100" || "02700" || "02500"):
    >
    > /*
    > rest of code......
    > */
    >
    > break;
    >
    > }
    >
    > ?>
    >
    > Does this work???? (i mean the || statement(s) in combination with a switch
    > statement?[/color]

    I don't know if your method works, but traditionally you made use of
    switch()'s fall through:

    switch($var) {
    case "02100":
    case "02700":
    case "02500":
    /* code */
    break;
    }

    HTH,

    Luke

    Comment

    • John Smith

      #3
      Re: Switch statement

      Luke Ross wrote:[color=blue]
      > Marcel wrote:
      >[color=green]
      >> <?php
      >>
      >> switch ($var) {
      >>
      >> case ("02100" || "02700" || "02500"):
      >>
      >> /*
      >> rest of code......
      >> */
      >>
      >> break;
      >>
      >> }
      >>
      >> ?>
      >>
      >> Does this work???? (i mean the || statement(s) in combination with a
      >> switch
      >> statement?[/color]
      >
      >
      > I don't know if your method works, but traditionally you made use of
      > switch()'s fall through:
      >
      > switch($var) {
      > case "02100":
      > case "02700":
      > case "02500":
      > /* code */
      > break;
      > }
      >
      > HTH,
      >
      > Luke
      >[/color]

      Also try to use single quotes instead of double, otherwise the PHP
      parser has to parse the contents twice.

      switch($var) {
      case '02100':
      case '02700':
      case '02500':
      /* code */
      break;
      }

      Grant.


      Comment

      • Marcel

        #4
        Re: Switch statement


        "Luke Ross" <lukeross@sys31 75.co.uk> schreef in bericht
        news:vpchgq56ub bu89@corp.super news.com...[color=blue]
        > Marcel wrote:[color=green]
        > > <?php
        > >
        > > switch ($var) {
        > >
        > > case ("02100" || "02700" || "02500"):
        > >
        > > /*
        > > rest of code......
        > > */
        > >
        > > break;
        > >
        > > }
        > >
        > > ?>
        > >
        > > Does this work???? (i mean the || statement(s) in combination with a[/color][/color]
        switch[color=blue][color=green]
        > > statement?[/color]
        >
        > I don't know if your method works, but traditionally you made use of
        > switch()'s fall through:
        >
        > switch($var) {
        > case "02100":
        > case "02700":
        > case "02500":
        > /* code */
        > break;
        > }
        >
        > HTH,
        >
        > Luke
        >[/color]

        Sorry for being so lazy...... i figured it out myself....

        <?php

        $var = "02700";

        switch ($var) {

        case ("02100" || "02700" || "02500"):

        echo "<HR>Yes this works ok!!! (\$var = $var)<HR>";

        break;

        }

        ?>

        So, it works!!!!

        Thanks anyway!!!!

        Marcel


        Comment

        • John Smith

          #5
          Re: Switch statement

          Marcel wrote:
          [color=blue]
          > "Luke Ross" <lukeross@sys31 75.co.uk> schreef in bericht
          > news:vpchgq56ub bu89@corp.super news.com...
          >[color=green]
          >>Marcel wrote:
          >>[color=darkred]
          >>><?php
          >>>
          >>>switch ($var) {
          >>>
          >>> case ("02100" || "02700" || "02500"):
          >>>
          >>> /*
          >>> rest of code......
          >>> */
          >>>
          >>> break;
          >>>
          >>>}
          >>>
          >>>?>
          >>>
          >>>Does this work???? (i mean the || statement(s) in combination with a[/color][/color]
          >
          > switch
          >[color=green][color=darkred]
          >>>statement?[/color]
          >>
          >>I don't know if your method works, but traditionally you made use of
          >>switch()'s fall through:
          >>
          >>switch($var ) {
          >> case "02100":
          >> case "02700":
          >> case "02500":
          >> /* code */
          >> break;
          >>}
          >>
          >>HTH,
          >>
          >>Luke
          >>[/color]
          >
          >
          > Sorry for being so lazy...... i figured it out myself....
          >
          > <?php
          >
          > $var = "02700";
          >
          > switch ($var) {
          >
          > case ("02100" || "02700" || "02500"):
          >
          > echo "<HR>Yes this works ok!!! (\$var = $var)<HR>";
          >
          > break;
          >
          > }
          >
          > ?>
          >
          > So, it works!!!!
          >
          > Thanks anyway!!!!
          >
          > Marcel
          >
          >[/color]

          Just like this works?

          <?php

          $var = "-12345";

          switch ($var) {

          case ("02100" || "02700" || "02500"):

          echo "<HR>Yes this works ok!!! (\$var = $var)<HR>";

          break;

          }

          ?>

          The result?

          "Yes this works ok!!! ($var = -12345)"

          .... No it doesn't... Read Luke Ross's post.

          Comment

          • Alan Little

            #6
            Re: Switch statement

            Carved in mystic runes upon the very living rock, the last words of
            Marcel of comp.lang.php make plain:
            [color=blue][color=green]
            >> Marcel wrote:[color=darkred]
            >> >
            >> > Does this work???? (i mean the || statement(s) in combination with
            >> > a switch statement?[/color]
            >>[/color]
            > Sorry for being so lazy...... i figured it out myself....
            >
            > So, it works!!!![/color]

            Testing is always the best way to get an answer to a question. But you
            have to test thoroughly: as John Smith pointed out, this construction
            will catch everything. You can see what's happening if you do this:

            if ("02100" || "02700" || "02500") {
            echo "True";
            }

            That expression will always evaluate to true. Your case statement then,
            is just a long way of writing

            case true:

            --
            Alan Little
            Phorm PHP Form Processor

            Comment

            Working...