Results 1 to 5 of 5
  1. #1
    Full Member gcarson's Avatar
    Join Date
    November 13th, 2009
    PHP Comparing 2 Strings
    OK. I hate asking simple programming questions but I'm almost out of hair to pull out. I've been trying combination after combination but can't seem to figure this simple thing out.. hoping someone might have an idea.

    Basically, I'm passing a strong of words to a function I wrote. The string passes correctly when I print it out. But, I then want to compare this string with a few strings to see which one matches, and if it matches, do certain things. So here's what I tried and numerous variations but can't seem to get it to work:

    PHP Code:
    function dosomething($var) {

    if (
    strcmp($var'First set of words') == 0) {
    ... do 
    }elseif (strcmp($var'Second set of words') == 0) {
    ... do 
    something else

    When I echo $var, it prints out 'First set of words' on the screen, but for whatever reason, I can't get it to see that they are the same. I've always tried something simple like if($var == 'First set of words') but that didn't work either.

    Any thoughts on what I might be doing wrong?

  2. #2
    ABW Ambassador superCool's Avatar
    Join Date
    April 23rd, 2008
    if($var == 'First set of words') should work if the values are equal. you might try to trim $var first to remove any leading or trailing spaces

  3. #3
    Full Member gcarson's Avatar
    Join Date
    November 13th, 2009
    Hmm.. tried trim before and tried it again, using the method above, which is what I've used in other places, still no luck. Does it have anything to do with the value $var coming in from the function? Or, this example uses an '&'. Do I need to treat that special? Meaning, its something like 'First & Second set of words'.

    I probably should have started with a simple one without a special character!

    Edit... when I look at the source, its print '&'.. My hairline doesn't need this!
    Last edited by gcarson; December 2nd, 2010 at 07:56 PM.

  4. #4
    Full Member
    Join Date
    November 21st, 2010
    your code works as expected, at least in my implementation of php (unsure of which 5.x it is offhand)

    function dosomething($var) {
    echo "<br>$var - ";
    if (strcmp($var, 'First set of words') == 0) echo '1';
    elseif (strcmp($var, 'Second set of words') == 0) echo '2';
    dosomething('First set of words');
    dosomething('first set of words');
    is exactly what I used. Note that strcmp is case sensitive so it passes the first, but not the second string.

    it's also possible that the strings don't match either but it may not be obvious. there's a great entry in the user contributed notes on PHP: strcmp - Manual which suggests using the md5() function on both strings to truly see if they match.

    but without seeing your exact code, that's the best I can offer.

    ETA: just saw your response. I went ahead and plugged in a & in the strings and they still matched as expected.
    Last edited by bradk; December 2nd, 2010 at 07:57 PM.

  5. #5
    Full Member gcarson's Avatar
    Join Date
    November 13th, 2009
    Yeah, I'm sorry. Like I thought it was something stupid I was missing. The example I was using had an & in it so I just typed the phrase in with an &. But, then I had the thought to check the actual source code and noticed the '&amp;'. So once I changed all & to '&amp;', and it worked fine using the simple method supercool had.

    Thanks for your help!

  6. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. OR / AND in Search Strings
    By dssc in forum GoldenCAN
    Replies: 0
    Last Post: October 29th, 2007, 06:12 PM
  2. gbot entering search strings????!!!
    By mrbshouse in forum Search Engine Optimization
    Replies: 6
    Last Post: July 17th, 2007, 03:54 PM
  3. Referrer Strings - CJ & SAS
    By FlyFisherman in forum Midnight Cafe'
    Replies: 0
    Last Post: August 15th, 2006, 10:03 AM
  4. Extracting query strings
    By happypoon in forum Midnight Cafe'
    Replies: 11
    Last Post: September 24th, 2003, 02:33 AM

Posting Permissions

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