Whats wrong with this code?

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

    Whats wrong with this code?

    $antroj=0;
    $dtctxsz1 = $_POST["dtctxsz1"];
    if($dtctxsz1 == 0){
    print $dtctxsz1;
    $antroj = $antroj + 1;
    }

    It adds 1 to $antroj even when the value of $dtctxsz1 is not 0. I have
    spent 7 hours on this one problem. Its driving me nuts.

    Garry Jones
    Sweden


  • halkeye@gmail.com

    #2
    Re: Whats wrong with this code?

    Off the top of my head, since you don't say what $dtctxsz1 is. I'd say
    its because $dtctxsz1 is blank or null, which "== 0" returns true for.

    you could try $dtctxsz1 === '' or !isset($dtctxsz 1).

    Gavin

    On Oct 5, 12:09 pm, "Garry Jones" <garry.jo...@mo rack.sewrote:
    $antroj=0;
    $dtctxsz1 = $_POST["dtctxsz1"];
    if($dtctxsz1 == 0){
    print $dtctxsz1;
    $antroj = $antroj + 1;
    >
    }It adds 1 to $antroj even when the value of $dtctxsz1 is not 0. I have
    spent 7 hours on this one problem. Its driving me nuts.
    >
    Garry Jones
    Sweden

    Comment

    • Garry Jones

      #3
      Re: Whats wrong with this code?

      <halkeye@gmail. comskrev i meddelandet
      news:1160075930 .560400.315630@ e3g2000cwe.goog legroups.com...
      Off the top of my head, since you don't say what $dtctxsz1 is. I'd say
      its because $dtctxsz1 is blank or null, which "== 0" returns true for.
      Here is some more of the offending code.

      First - Exerpt from a form.

      <select name="dtctxsz1" class="vtredsta r" id="dtctxsz1">
      <option value="0" selected>Ej vald</option>
      <option value="xs">XS</option>
      <option value="s">S</option>
      <option value="m">M</option>
      <option value="l" >L</option>
      <option value="xl">XL</option>
      <option value="xxl">XXL </option>
      <option value="xxxl">XX XL</option>
      </select>

      Processed by.....

      $antroj = 0;
      $dtctxsz1 = $_POST["dtctxsz1"];
      if($dtctxsz1 == 0){
      print $dtctxsz1;
      $antroj = $antroj + 1;
      }

      If the user chooses xl the value of $dtctxsz1 is xl.

      It should not then execute the print command because $dtctxsz1 is not 0.

      However it prints, and it prints the value the user clicked on - in this
      case xl

      It should not be adding one to the value of $antroj should it. But it is!

      If the value was misfiring it would be printing 0 because it can only print
      when the value is 0. So I am missing something here!

      Greatful for any help in this matter.

      Garry Jones
      Sweden


      Comment

      • Pedro Graca

        #4
        Re: Whats wrong with this code?

        Garry Jones wrote:
        $antroj=0;
        $dtctxsz1 = $_POST["dtctxsz1"];
        if($dtctxsz1 == 0){
        print $dtctxsz1;
        $antroj = $antroj + 1;
        }
        >
        It adds 1 to $antroj even when the value of $dtctxsz1 is not 0. I have
        spent 7 hours on this one problem. Its driving me nuts.
        It works for me. Maybe some of the code you didn't post is the culprit.

        <?php
        error_reporting (E_ALL);
        ini_set('displa y_errors', '1');

        $antroj = 0;
        $dtctxsz1 = '14'; // $_POST["dtctxsz1"];
        if ($dtctxsz1 == 0) {
        // print $dtctxsz1;
        ++$antroj;
        }
        echo 'after \'14\' $antroj is ', $antroj, ".\n<br>";

        $dtctxsz1 = '0'; // $_POST["dtctxsz1"];
        if ($dtctxsz1 == 0) {
        // print $dtctxsz1;
        ++$antroj;
        }
        echo 'after \'0\' antroj is ', $antroj, ".\n<br>";

        $dtctxsz1 = 'test'; // $_POST["dtctxsz1"];
        if ($dtctxsz1 == 0) {
        // print $dtctxsz1;
        ++$antroj;
        }
        echo 'after \'test\' $antroj is ', $antroj, ".\n<br>";

        ?>

        Comment

        • Gary Hasler

          #5
          Re: Whats wrong with this code?

          Pedro Graca wrote:
          It works for me. Maybe some of the code you didn't post is the culprit.
          I agree. Or else (this is a long shot) maybe you accidentally used a
          numeral one "1" (instead of lower case L) in the variable name in the if
          statement, being undefined it's somehow equating to zero?

          Comment

          • Pedro Graca

            #6
            Re: Whats wrong with this code?

            Garry Jones wrote:
            First - Exerpt from a form.
            >
            <select name="dtctxsz1" class="vtredsta r" id="dtctxsz1">
            <option value="0" selected>Ej vald</option>
            <option value="xs">XS</option>
            [...]
            </select>
            >
            Processed by.....
            >
            $antroj = 0;
            $dtctxsz1 = $_POST["dtctxsz1"];
            if($dtctxsz1 == 0){
            print $dtctxsz1;
            $antroj = $antroj + 1;
            }
            if ('fortytwo' == 0) echo 'Douglas Adams had it wrong!';

            You should have a look at the Type Juggling manual page:



            <?php
            header('Content-Type: text/plain');
            error_reporting (E_ALL);
            ini_set('displa y_errors', '1');

            $x1 = '0';
            $x2 = 'xs';
            $x3 = 0;

            $tests = array();
            $tests[] = 'return $x1 == $x2;';
            $tests[] = 'return $x1 === $x2;';
            $tests[] = 'return $x1 == $x3;';
            $tests[] = 'return $x1 === $x3;';
            $tests[] = 'return $x2 == $x3;';
            $tests[] = 'return $x2 === $x3;';
            $tests[] = 'return $x1 == 0;';
            $tests[] = 'return $x1 === 0;';
            $tests[] = 'return $x1 == \'0\';';
            $tests[] = 'return $x1 === \'0\';';
            $tests[] = 'return $x2 == 0;';
            $tests[] = 'return $x2 === 0;';
            $tests[] = 'return $x2 == \'0\';';
            $tests[] = 'return $x2 === \'0\';';
            $tests[] = 'return $x3 == 0;';
            $tests[] = 'return $x3 === 0;';
            $tests[] = 'return $x3 == \'0\';';
            $tests[] = 'return $x3 === \'0\';';

            echo '$x1 = \'0\';
            $x2 = \'xs\';
            $x3 = 0;

            ';
            $n = 0;
            foreach ($tests as $v) {
            echo $v, "\treturns ", (eval($v)) ? ('true') : ('false'), "\n";
            }
            ?>

            --
            File not found: (R)esume, (R)etry, (R)erun, (R)eboot

            Comment

            • Jeff North

              #7
              Re: Whats wrong with this code?

              On Thu, 5 Oct 2006 22:06:57 +0200, in comp.lang.php "Garry Jones"
              <garry.jones@mo rack.se>
              <eg3ohc$le4$1@y ggdrasil.glocal net.netwrote:
              >| <halkeye@gmail. comskrev i meddelandet
              >| news:1160075930 .560400.315630@ e3g2000cwe.goog legroups.com...
              >| Off the top of my head, since you don't say what $dtctxsz1 is. I'd say
              >| its because $dtctxsz1 is blank or null, which "== 0" returns true for.
              >|
              >| Here is some more of the offending code.
              >|
              >| First - Exerpt from a form.
              >|
              >| <select name="dtctxsz1" class="vtredsta r" id="dtctxsz1">
              >| <option value="0" selected>Ej vald</option>
              >| <option value="xs">XS</option>
              >| <option value="s">S</option>
              >| <option value="m">M</option>
              >| <option value="l" >L</option>
              >| <option value="xl">XL</option>
              >| <option value="xxl">XXL </option>
              >| <option value="xxxl">XX XL</option>
              >| </select>
              >|
              >| Processed by.....
              >|
              >| $antroj = 0;
              >| $dtctxsz1 = $_POST["dtctxsz1"];
              >| if($dtctxsz1 == 0){
              >| print $dtctxsz1;
              >| $antroj = $antroj + 1;
              >| }
              The select control returns a string value so you should change your
              code to:
              if($dtctxsz1 == "0"){
              >| If the user chooses xl the value of $dtctxsz1 is xl.
              >|
              >| It should not then execute the print command because $dtctxsz1 is not 0.
              >|
              >| However it prints, and it prints the value the user clicked on - in this
              >| case xl
              >|
              >| It should not be adding one to the value of $antroj should it. But it is!
              >|
              >| If the value was misfiring it would be printing 0 because it can only print
              >| when the value is 0. So I am missing something here!
              >|
              >| Greatful for any help in this matter.
              >|
              >| Garry Jones
              >| Sweden
              >|
              ---------------------------------------------------------------
              jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
              ---------------------------------------------------------------

              Comment

              • Garry Jones

                #8
                Re: Whats wrong with this code?

                Gary Hasler" <garyhasler@the logconnection.c omskrev i meddelandet
                news:45256F6B.D E571CC4@thelogc onnection.com.. .
                I agree. Or else (this is a long shot) maybe you accidentally used a
                numeral one "1" (instead of lower case L) in the variable name in the if
                statement, being undefined it's somehow equating to zero?
                Thanks to all.

                Jeff hit the nail on the head....

                The select control returns a string value so you should change your
                code to:
                if($dtctxsz1 == "0"){

                Amazing what a pair of quotation marks can do.

                Garry Jones
                Sweden


                Comment

                Working...