PHP If String Comparison Question

Discussion in 'PHP' started by phaze3131, May 2, 2009.

  1. #1
    Hey guys on my wordpress blog I am trying to compare the following

    if ($category[0]->cat_name == "television" )

    in wordpress the $category[] is the array of categories a post has, and cat-name i just the actual name of that category.

    If I echo $category[0]->cat_name it is television

    but I can't get this to flag as true for the if statement, something is wrong and it keeps doing the ELSE part.

    Anyone know what is wrong here?


    thank you alot for any help

    Travis
     
    phaze3131, May 2, 2009 IP
  2. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try attribute_escape($category[0]->cat_name) ??
     
    GreatMetro, May 2, 2009 IP
  3. phaze3131

    phaze3131 Well-Known Member

    Messages:
    538
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    140
    #3
    That didn't seem to work. I'm trying it using 'television' and "television"
     
    phaze3131, May 2, 2009 IP
  4. AustinQPT

    AustinQPT Member

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    $category[0]->cat_name = $cat;
    if ($cat == "television"){
    echo"worked";
    }else{
    echo"didn't work";
    }


    Try this. If it still doesn't work try not using exactly equal to just one qual sign because some spaces may be messing it up.
     
    AustinQPT, May 2, 2009 IP
  5. WoRLDLiFE

    WoRLDLiFE Peon

    Messages:
    116
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    try this
    
    if (strtolower($category[0]->cat_name)=="television")
    {
       // your expression if works
    }
    else
    {
       // your expression if it doesn't
    }
    
    PHP:
     
    WoRLDLiFE, May 3, 2009 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    php might have treated $category[0]->cat_name as an object, rather than a string.

    You can try typecast it.

    
    if ((string) $category[0]->cat_name == "television" ) {
       //
    }
    
    PHP:
     
    Kaizoku, May 3, 2009 IP