SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    == and === (Strange Error)

    PHP5/IIS/Windows 2003/Smarty

    I have 2 conditions
    PHP Code:
    $value 1;
    $selected 2;
    if((
    $value === $selected) )
    {
        echo  
    'TRUE'//DOESN'T work, which is correct
    }
    if((
    $value == $selected) )
    {
        echo  
    'TRUE'//WORKS, which is WRONG

    The 2nd condition should evaluate to false but it's evaluating to true.

    I am not sure why, but it's driving me mad.

    Please help
    ---------------------------
    Errors = Improved Programming.
    My Site

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $a 1;
    $b 2;
    var_dump($a == $b); 
    Outputs false hmmm
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana View Post
    PHP5/IIS/Windows 2003/Smarty

    I have 2 conditions
    PHP Code:
    $value 1;
    $selected 2;
    if((
    $value === $selected) )
    {
        echo  
    'TRUE'//DOESN'T work, which is correct
    }
    if((
    $value == $selected) )
    {
        echo  
    'TRUE'//WORKS, which is WRONG

    The 2nd condition should evaluate to false but it's evaluating to true.

    I am not sure why, but it's driving me mad.

    Please help
    #
    First theres no need for the double brackets.

    PHP Code:
    $value 1;
    $selected 2;

    if (
    $value == $selected )
    {
        echo  
    'Second one TRUE'//WORKS, which is WRONG
    }

    if (
    $value === $selected )
    {
        echo 
    'First one TRUE'//DOESN'T work, which is correct

    Try that, not sure if it will make much of a difference, could be a tedious error, or might have been recognised as a ternary statement.

    (p.s. if your testing its fine; but if you want to return a true/false boolean expression make sure you return it and don't echo it.)
    Kieran Allen, 16 years old
    Full time PHP developer,
    With 5 years experience in web development.
    www.kieran.in .. my blog.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just making sure, your not doing this inside a function and trying to get a boolean value back are you?
    Kieran Allen, 16 years old
    Full time PHP developer,
    With 5 years experience in web development.
    www.kieran.in .. my blog.

  5. #5
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    I am just debugging (with echo statements) as the function wasn't returning correct values

    Now I tried your code with SPECIFIC values MANUALLY entered (1 and 2) and it worked as expected.

    But, when I use as it as before (with values via parameters) it doesn't work fine.

    Following is the output


    2 == 2_2
    Second one TRUE


    as you can see, both values (I just printed those inside the == IF statement, just to make sure) are NOT equal, yet it's still computing it as true.



    BTW, the function I am trying is a Smarty Plugin (http://smarty.incutio.com/?page=select) inside my template.
    ---------------------------
    Errors = Improved Programming.
    My Site

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    2_2 gets converted to an integer so 2 == 2 == true
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But, Why would it gt converted to an integer?

    There is no casting function and I am just comparing string.
    ---------------------------
    Errors = Improved Programming.
    My Site

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try...

    PHP Code:
    $value =(int) 1;

    $selected =(int) 2;



    if (
    $value == $selected )

    {

        echo  
    'Second one TRUE'//WORKS, which is WRONG

    }



    if (
    $value === $selected )

    {

        echo 
    'First one TRUE'//DOESN'T work, which is correct


    Kieran Allen, 16 years old
    Full time PHP developer,
    With 5 years experience in web development.
    www.kieran.in .. my blog.

  9. #9
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried that and even WITHOUT (int), it works fine.

    Which means BOTH are FALSE.
    ---------------------------
    Errors = Improved Programming.
    My Site

  10. #10
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    when you test a string against an integer it is converted to an integer.
    This is not the case with === as it also test type.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  11. #11
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah I see.

    But, isn't PHP loose type? I guess it does all of it in background then? because for compiler their types are different?
    ---------------------------
    Errors = Improved Programming.
    My Site

  12. #12
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two values of different types can't be compared as-is. Either the first or the second one needs to be converted. When values are a string and an integer, string value is converted to integer.

  13. #13
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    This variable and array tests might shed some light for you.

    PHP thinks 1 and "1thing" are the same value (n.b. that is to say, they are not identical but when interpreted their values are the same ...) ,well PHP has to start somewhere when comparing what could be ridiculous values ... so it seems to start with integers.

    We get lots of benefits - but we really need to appreciate the rules being applied "behind the scenes" as it were.

    The matrix above, if nothing else, should scream out three things:

    1. Don't take anything for granted when using comparison operators 'till, well, 'till I guess till you've learned them
    2. Use var_dump() when you are trying to figure out wtf is going wrong
    3. Print off that page and nail it to the wall


    Learn to love ===

  14. #14
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    That page is ....interesting
    ---------------------------
    Errors = Improved Programming.
    My Site


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •