Why strange IF...ELSE behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J-P

    Why strange IF...ELSE behavior

    Hi all,

    I'm getting a strange result with the following IF statement:

    $bar = ($foo == 'last') ? true : false;

    In my script $foo normaly has a integer value greater than 0, but can have
    last as value. But whatever value of $foo $bar always becomes true.
    When I use the regular if {....}else{.... } I get the same result, always
    $bar becomes true.
    I've played around with a couple things, like turning the statement around
    ($foo != 'last') but nothing seems to give the desired result.
    Can anyone tell me what I'm doing wrong or how to fix this.

    Thanx,
    J-P


  • P'tit Marcel

    #2
    Re: Why strange IF...ELSE behavior

    J-P écrivit:
    [color=blue]
    > Hi all,
    >
    > I'm getting a strange result with the following IF statement:
    >
    > $bar = ($foo == 'last') ? true : false;
    >
    > In my script $foo normaly has a integer value greater than 0, but can
    > have last as value. But whatever value of $foo $bar always becomes
    > true.[/color]

    It's due to the silent type casting of php
    you should use === instead of ==


    --
    P'tit Marcel

    Comment

    • J-P

      #3
      Re: Why strange IF...ELSE behavior

      [color=blue]
      >
      > It's due to the silent type casting of php
      > you should use === instead of ==
      >[/color]

      Thanx that did it, never to late to learn something.


      Comment

      Working...