Support » Everything else WordPress » Comparing strings doesn't work !?!?

  • Resolved pikkabbu


    First, let me tell you that I’m modifying a theme’s file and I don’t know php.

    Two: I know others programing languages, so I’m not completely dummy.

    I need to modify just a bit the page that displays the portfolio, so that only pictures belonging to a selected category will be shown.

    But the test
    If $myvariable = “mystring” fails, even if both appears to be the same string.

    Let me write down the actual code that I wrote just to check if the if test works (and it doesn’t):

    $mycategory = get_the_term_list( $post_id, 'portfolio_category', '', ', ', '' ) ;
    echo $mycategory ; // output is portrait !!!!
    If ($mycategory == "portrait") { echo " correct"  ; }
    else { echo " it fails" ; }

    The result is “it fails”
    That is: on the screen, $mycategory appears to be “portrait” as expected, but the comparation with string “portrait” fails.

    It’s getting me crazy.

    Any help ?


    [Please post code snippets between backticks or use the code button.]

Viewing 6 replies - 1 through 6 (of 6 total)
  • esmi


    Forum Moderator

    Returns an HTML string of taxonomy terms associated with a post and given taxonomy. Terms are linked to their respective term listing pages.

    The bold is mine. 🙂



    Thanks Esmi.

    In fact, I’m in the same situation than before 🙁

    that is: How do I compare a…. html string to a text string ?

    I guess there is any converting function, something like

    $mytruecategory = convertion($myhtmlcategory) but I can’t find it out.

    I’ve seen a lot of convertions, but I don’t understand what they do and which to use.

    Still need some help.

    Yhank you



    Forum Moderator

    How do I compare a…. html string to a text string

    If it was me, the first thing I’d do is examine exactly what $mycategory holds by using

    echo '<pre>';
    echo '</pre>';

    and if necessary, checking the source code of the output for any HTML tags. Then use strip_tags() to remove any HTML tags from the returned value(s) and, if necessary, trim() to ensure that I was comparing like with like.



    Thank you very much for your quick answer !!

    Now I’m going to work the way you say.

    I’ll get back here and tell how I manage 😉

    But… it’s 1 o’clock in the morning, and my eyes are saying “pleeeeaaaase stop”.

    I’ll have to way until tomorrow.



    I couldn’t wait !

    I just wanted to check your code and…
    discovered that $mycategory had more than only letters.

    This has been important !

    Afterwards, I tried strip_tags and ta tchannnn : it works, and now my if test works too.

    Solved problem.

    Thanks so much !!!
    I can now go and sleep happy.



    Forum Moderator

    Yay! Glad I could help 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Comparing strings doesn't work !?!?’ is closed to new replies.