Problem comparing strings from arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ZeroFear
Forum Newbie
Posts: 14
Joined: Tue Feb 14, 2006 10:47 pm

Problem comparing strings from arrays

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
ZeroFear
Forum Newbie
Posts: 14
Joined: Tue Feb 14, 2006 10:47 pm

Post by ZeroFear »

i tried == but it dosnt work correctly
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
themurph
Forum Commoner
Posts: 76
Joined: Wed Apr 19, 2006 1:56 pm
Contact:

Post by themurph »

trim() each string for the comparison mabye?

You may have leading/trailing spaces in the strings.
Post Reply