Page 1 of 1
Problem comparing strings from arrays
Posted: Mon Jul 31, 2006 3:18 pm
by ZeroFear
I am comparing 2 strings that are being returned from arrays, and i want the if statement to execute if the strings are different. In this code snip this particular IF statement should only be true 4 times, but it keeps geting entered somehow. What am i doing wrong here? I tried != , !==, and now im doing strcmp. Here is the code
Code: Select all
if ( strcmp( $quarter[$i] , $groups[1][$i] ) != 0 ) {
$quarter[$i] = $groups[1][$i];
}
If you need to see the rest of the code, it is located here:
http://www.btcampaign.com/blog/code.zip
Posted: Mon Jul 31, 2006 3:28 pm
by jayshields
I can't see what you're trying to do in your post, but comparing strings is fairly straight forward...
Code: Select all
if($string1 == $string2) { //if string1 is the same as string2 then
//execute something
}
if($string1 != $string2) { //if string1 is not the same as string2 then
//execute something
}
Posted: Mon Jul 31, 2006 4:33 pm
by Ollie Saunders
Yeah that is typical of the kind of stuff I would try and do when I first started learning PHP from a C++ background (even if it was a short one).
Generally ZeroFear in PHP things are much easier xD there are loada functions that mean you don't have to reinvent the wheel and the arrays are really powerful.
Posted: Mon Jul 31, 2006 4:58 pm
by ZeroFear
i tried == but it dosnt work correctly
Posted: Mon Jul 31, 2006 5:02 pm
by RobertGonzalez
First thing I recommend is to echo out the array vals before any comparisons as a test just to see if the values coming into your comparison are what you expect. If they are the values you expect, then the next step is determining the logic to write the proper comparisons.
Posted: Mon Jul 31, 2006 5:45 pm
by themurph
trim() each string for the comparison mabye?
You may have leading/trailing spaces in the strings.