SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a weird one... I have an if statement where I'm printing out the results of an sql query displaying both
    $cat and $link[cid]

    Now, when I print those from inside the if statement, they should be the same right?? For some reason, they aren't.

    So to test it, I printed them outside the "if" as well and it works fine.

    PHP Code:
    $link=(mysql_fetch_array($result));
    $cat $link[CID];

    print 
    "cat: $cat:$link[CID], ";

    if (
    $cat "4") {
    print 
    "cat $cat:$link[CID]";

    This yields the results:

    cat: 2:2, cat 4:2

    No matter what I make $cat = to in the if statement, it somehow resets the variable $cat when it prints to what I'm asking for in the if statement -- so the result is always true even though it shouldn't be. In other words, they should all be 2's.

    any ideas why this is happening?
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In PHP (and the C language which it is based on) = is the assignment operator and does not also operate as an equality operator which is ==

    So your if statement
    PHP Code:
    if*($cat*=*"4"
    is assigning "4" to $cat. Another side effect of this logic error is that your if statement should always be entered, even when $cat does not equal "4". You might already know this and have just made a silly mistake. I do it too .

    Correct code
    PHP Code:
    if*($cat*==*"4")*{
       
    print*"cat*$cat:$link[CID]";


  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2001
    Location
    Monmouth Junction, NJ
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep everytime you have if statements and are comparing, you should use == instead of =

  4. #4
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doh... I knew it was something stupid!! I have some other if statements in the script with == but for some reason, I just missed it in this one. Works fine now.

    Thanks again!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net


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
  •