Arithmetic error involving 0

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

    Arithmetic error involving 0

    I get the output:

    age=0;

    age=0;

    from the following code:

    $debug .= "<br>age=$age;< br>";
    if($age!="unk") $age = $age+1;
    $debug .= "<br>age=$age;< br>";

    What is going on?
  • dspohn

    #2
    Re: Arithmetic error involving 0

    why do you have semicolons after $age inside of the quotations?

    remove those, and see if you still get the error.

    Comment

    • Chris Hope

      #3
      Re: Arithmetic error involving 0

      Bob Stearns wrote:
      [color=blue]
      > I get the output:
      >
      > age=0;
      >
      > age=0;
      >
      > from the following code:
      >
      > $debug .= "<br>age=$age;< br>";
      > if($age!="unk") $age = $age+1;
      > $debug .= "<br>age=$age;< br>";
      >
      > What is going on?[/color]

      IIRC there are some odd issues in PHP when comparing 0 with a string. If
      you change it to do a strict type check with !== it solves the
      immediate issue:

      if($age!=="unk" ) $age = $age+1;

      --
      Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

      Comment

      • Chris Hope

        #4
        Re: Arithmetic error involving 0

        dspohn wrote:
        [color=blue]
        > why do you have semicolons after $age inside of the quotations?
        >
        > remove those, and see if you still get the error.[/color]

        And why do you think that would make a difference?

        --
        Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

        Comment

        • Chris Hope

          #5
          Re: Arithmetic error involving 0

          Chris Hope wrote:
          [color=blue]
          > Bob Stearns wrote:
          >[color=green]
          >> I get the output:
          >>
          >> age=0;
          >>
          >> age=0;
          >>
          >> from the following code:
          >>
          >> $debug .= "<br>age=$age;< br>";
          >> if($age!="unk") $age = $age+1;
          >> $debug .= "<br>age=$age;< br>";
          >>
          >> What is going on?[/color]
          >
          > IIRC there are some odd issues in PHP when comparing 0 with a string.
          > If you change it to do a strict type check with !== it solves the
          > immediate issue:
          >
          > if($age!=="unk" ) $age = $age+1;[/color]

          I found the reason.

          From: http://www.php.net/manual/en/languag...comparison.php

          "If you compare an integer with a string, the string is converted to a
          number. If you compare two numerical strings, they are compared as
          integers."

          var_dump(0 == "a"); // 0 == 0 -> true
          var_dump("1" == "01"); // 1 == 1 -> true

          Unless it is at all numerical the string will be converted to 0 making a
          comparison with 0 true. In your example the comparison ends up being
          0 != 0 which is of course false.

          --
          Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

          Comment

          • Bob Stearns

            #6
            Re: Arithmetic error involving 0

            Chris Hope wrote:[color=blue]
            > Chris Hope wrote:
            >
            >[color=green]
            >>Bob Stearns wrote:
            >>
            >>[color=darkred]
            >>>I get the output:
            >>>
            >>>age=0;
            >>>
            >>>age=0;
            >>>
            >>>from the following code:
            >>>
            >>>$debug .= "<br>age=$age;< br>";
            >>>if($age!="un k") $age = $age+1;
            >>>$debug .= "<br>age=$age;< br>";
            >>>
            >>>What is going on?[/color]
            >>
            >>IIRC there are some odd issues in PHP when comparing 0 with a string.
            >>If you change it to do a strict type check with !== it solves the
            >>immediate issue:
            >>
            >>if($age!=="un k") $age = $age+1;[/color]
            >
            >
            > I found the reason.
            >
            > From: http://www.php.net/manual/en/languag...comparison.php
            >
            > "If you compare an integer with a string, the string is converted to a
            > number. If you compare two numerical strings, they are compared as
            > integers."
            >
            > var_dump(0 == "a"); // 0 == 0 -> true
            > var_dump("1" == "01"); // 1 == 1 -> true
            >
            > Unless it is at all numerical the string will be converted to 0 making a
            > comparison with 0 true. In your example the comparison ends up being
            > 0 != 0 which is of course false.
            >[/color]
            I saw that some time ago and forgot it. Thanks for the reminder.

            Comment

            • Bob Stearns

              #7
              Re: Arithmetic error involving 0

              Chris Hope wrote:
              [color=blue]
              > Bob Stearns wrote:
              >
              >[color=green]
              >>I get the output:
              >>
              >>age=0;
              >>
              >>age=0;
              >>
              >>from the following code:
              >>
              >>$debug .= "<br>age=$age;< br>";
              >>if($age!="unk ") $age = $age+1;
              >>$debug .= "<br>age=$age;< br>";
              >>
              >>What is going on?[/color]
              >
              >
              > IIRC there are some odd issues in PHP when comparing 0 with a string. If
              > you change it to do a strict type check with !== it solves the
              > immediate issue:
              >
              > if($age!=="unk" ) $age = $age+1;
              >[/color]
              I saw that some time ago and forgot it. Thanks for the reminder.

              Comment

              Working...