Problem with 'switch' and zero

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

    Problem with 'switch' and zero

    I'm trying to use a switch construct to compare the result of a form
    with a list of known values and display a different page according to
    the submitted string. The problem is that some of the answers I need to
    check for are strings of the character zero of varying length:

    E.g.
    <?php

    switch (trim(strtolowe r($_POST['answer']))):

    case "abc":
    case "xyz":
    case "0":
    include 'close.htm';
    break;
    case "0000000000 ":
    include 'vclose.htm';
    break;
    default:
    include 'sorry.htm';
    endswitch;
    ?>

    What happens is that a submission consisting of a string of any number
    of zeroes cause the first page to be displayed rather than the second. I
    thought this was because it was treating it as an integer 0, so I tried
    putting (string) casts all over the place, which didn't help.

    Could someone please tell me how to achieve what I want?
    --
    Steve Loft
  • Pedro Graca

    #2
    Re: Problem with 'switch' and zero

    Steve Loft wrote:[color=blue]
    > I'm trying to use a switch construct to compare the result of a form
    > with a list of known values and display a different page according to
    > the submitted string. The problem is that some of the answers I need to
    > check for are strings of the character zero of varying length:[/color]
    [color=blue]
    > I thought this was because it was treating it as an integer 0, so I
    > tried putting (string) casts all over the place, which didn't help.[/color]

    No. PHP switch()s compare with "=="

    and, for PHP
    "000000" == "0"
    is true.

    You have to turn that switch into a series of if()s -- AFAIK there is no
    way to do "000000" === "0" (which if false) inside the switch() construct

    so, instead of
    switch(somethin g):
    case "abc":
    ...


    do
    if (something === "abc") ...
    elseif (something === "0") ...
    elseif (something === "000000") ...
    ...
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Steve Loft

      #3
      Re: Problem with 'switch' and zero

      Pedro Graca wrote:
      [color=blue]
      > You have to turn that switch into a series of if()s -- AFAIK there is no
      > way to do "000000" === "0" (which if false) inside the switch() construct[/color]

      Ah. OK, thanks very much, even if it wasn't good news. That seems pretty
      stupid of PHP to me, but anyway...
      --
      Steve Loft

      Comment

      • Steve Loft

        #4
        Re: Problem with 'switch' and zero

        Pedro Graca wrote:[color=blue]
        >
        > You have to turn that switch into a series of if()s -- AFAIK there is no
        > way to do "000000" === "0" (which if false) inside the switch() construct[/color]

        I've tried this, and I get the same result. I've even tried putting
        string casts in, and I've tried single quotes and double quotes:

        $ans = (string)trim(st rtolower($_POST['answer']));

        if ((string)$ans == (string)"0") {
        include 'close.htm';
        } elseif ((string)$ans == (string)"000000 0000") {
        include 'vclose.htm';
        } else {
        include 'sorry.htm';
        }

        If the form sends 0000000000, then the 'close.htm' file gets displayed.
        This is my first attempt at writing PHP and it's driving me insane -
        what am I doing wrong?
        --
        Steve Loft

        Comment

        • Steve Loft

          #5
          Re: Problem with 'switch' and zero

          I wrote:
          [color=blue]
          > I've tried this, and I get the same result. I've even tried putting
          > string casts in, and I've tried single quotes and double quotes:[/color]

          Pedro,

          Sorry. Sorry. Sorry. I didn't RTFM. I didn't realise what you meant when
          you used the "===" comparison. I now have it working with if...elseif...
          Thanks very much!
          --
          Steve Loft

          Comment

          • Pedro Graca

            #6
            Re: Problem with 'switch' and zero

            Steve Loft wrote:[color=blue]
            > I didn't realise what you meant when you used the "===" comparison.[/color]

            I call that the "very equal" comparison
            and similarly, the "!==" I call the "very different" comparison :)

            so I read this
            if ($a === false) whatever();

            as "if a is very false" do whatever
            [color=blue]
            > I now have it working with if...elseif...
            > Thanks very much![/color]

            You're welcome
            --
            --= my mail box only accepts =--
            --= Content-Type: text/plain =--
            --= Size below 10001 bytes =--

            Comment

            • Geoff Berrow

              #7
              Re: Problem with 'switch' and zero

              I noticed that Message-ID:
              <c2pgs7$1vjaon$ 2@ID-203069.news.uni-berlin.de> from Pedro Graca
              contained the following:
              [color=blue][color=green]
              >> I didn't realise what you meant when you used the "===" comparison.[/color]
              >
              >I call that the "very equal" comparison
              >and similarly, the "!==" I call the "very different" comparison :)[/color]

              Not "barely different"?

              --
              Geoff Berrow (put thecat out to email)
              It's only Usenet, no one dies.
              My opinions, not the committee's, mine.
              Simple RFDs http://www.ckdog.co.uk/rfdmaker/

              Comment

              • CountScubula

                #8
                Re: Problem with 'switch' and zero

                "Steve Loft" <steve@nybbles. co.uk> wrote in message
                news:7lj4i1-kml.ln1@galena. nybbles.co.uk.. .[color=blue]
                > I'm trying to use a switch construct to compare the result of a form
                > with a list of known values and display a different page according to
                > the submitted string. The problem is that some of the answers I need to
                > check for are strings of the character zero of varying length:
                >
                > E.g.
                > <?php
                >
                > switch (trim(strtolowe r($_POST['answer']))):
                >
                > case "abc":
                > case "xyz":
                > case "0":
                > include 'close.htm';
                > break;
                > case "0000000000 ":
                > include 'vclose.htm';
                > break;
                > default:
                > include 'sorry.htm';
                > endswitch;
                > ?>
                >
                > What happens is that a submission consisting of a string of any number
                > of zeroes cause the first page to be displayed rather than the second. I
                > thought this was because it was treating it as an integer 0, so I tried
                > putting (string) casts all over the place, which didn't help.
                >
                > Could someone please tell me how to achieve what I want?
                > --
                > Steve Loft[/color]

                just a hunch, a bandaid if you will. I have this problem in vb apps with
                keys, keys that start with numbers are not allowed in collections, so I
                preface them.

                switch "_" . (trim(strtolowe r($_POST['answer']))):
                {
                case "_0":

                case "_000000000 0":
                }

                --
                Mike Bradley
                http://www.gzentools.com -- free online php tools


                Comment

                Working...