how can one variable equal 2 things??

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

    how can one variable equal 2 things??

    I'm having a problem that is baffling me... say I set a session variable
    as so:

    $_SESSION['temp'] = "default";

    Now if I test the following two conditionals:

    if($_SESSION['temp'] == "default")

    and

    if($_SESSION['temp'] == 0)

    they both return TRUE. I don't understand why this is... if anything, I
    would have thought testing if it == 1 would be a boolean true since the
    variable exists and is not NULL, but testing if == 1 returns FALSE.
    Thanks in advance.
  • ZeldorBlat

    #2
    Re: how can one variable equal 2 things??

    When (non-numeric) strings are used in the context of a number (your ==
    0 comparison above) they evaluate to zero. Hence, both of your tests
    evaluate to true.

    Check out:



    Comment

    • Michael Winter

      #3
      Re: how can one variable equal 2 things??

      On 02/07/2005 23:34, Marcus wrote:

      [snip]
      [color=blue]
      > if anything, I would have thought testing if ['default'] == 1 would
      > be a boolean true since the variable exists and is not NULL, but
      > testing if ['default'] == 1 returns FALSE.[/color]

      This is due to implicit type conversion. When you use the equality (==)
      rather than strict equality (===) and compare two different types, the
      values are converted into something that can be compared in some
      meaningful way.

      If you compare a string to a number, an attempt is made to convert that
      string to a number first. If the string starts with valid number data,
      then that will be the used value, otherwise the comparison will be
      against zero (0).

      The string 'default' doesn't have anything resembling a number at its
      start, so it will be converted to zero. You then perform a comparison
      against literal zero, hence the expression evaluates to true.

      See the type comparison page[1], as well as the section on converting
      strings to numbers[2], in the manual.

      Mike


      [1] <URL:http://www.php.net/manual/en/types.compariso ns.php>
      [2]
      <URL:http://www.php.net/manual/en/language.types. string.php#lang uage.types.stri ng.conversion>

      --
      Michael Winter
      Prefix subject with [News] before replying by e-mail.

      Comment

      • Marcus

        #4
        Re: how can one variable equal 2 things??

        Michael Winter wrote:[color=blue]
        > On 02/07/2005 23:34, Marcus wrote:
        >
        > [snip]
        >[color=green]
        >> if anything, I would have thought testing if ['default'] == 1 would be
        >> a boolean true since the variable exists and is not NULL, but testing
        >> if ['default'] == 1 returns FALSE.[/color]
        >
        >
        > This is due to implicit type conversion. When you use the equality (==)
        > rather than strict equality (===) and compare two different types, the
        > values are converted into something that can be compared in some
        > meaningful way.
        >
        > If you compare a string to a number, an attempt is made to convert that
        > string to a number first. If the string starts with valid number data,
        > then that will be the used value, otherwise the comparison will be
        > against zero (0).
        >
        > The string 'default' doesn't have anything resembling a number at its
        > start, so it will be converted to zero. You then perform a comparison
        > against literal zero, hence the expression evaluates to true.
        >
        > See the type comparison page[1], as well as the section on converting
        > strings to numbers[2], in the manual.
        >
        > Mike
        >
        >
        > [1] <URL:http://www.php.net/manual/en/types.compariso ns.php>
        > [2]
        > <URL:http://www.php.net/manual/en/language.types. string.php#lang uage.types.stri ng.conversion>
        >
        >[/color]

        Thank you both for the links and info, I enclosed my 0 in quotes and
        everything now works.

        Comment

        Working...