SureshKumar Vegesna
SureshKumar Vegesna

Reputation: 1202

how to compare case insensitive two strings in php

I need to compare two string case insensitive here is my code

if(strcasecmp($genderseek,"both")==0)
{
  $gender2="$Ugender=='MALE'||$Ugender=='FEMALE'";
}
else
{
  $gender2.="$genderseek==$Ugender";
}

this code n't working for me any one help me

thanks in advance

Upvotes: 0

Views: 942

Answers (2)

Jon Gjengset
Jon Gjengset

Reputation: 4236

First of all, in the first branch, your code should use $gender2.= instead of just $gender2=

Second, if this is building an SQL query, which it looks like, then you might want to use the SQL functions LOWER or UPPER to do the comparison with MALE and FEMALE, depending on your dataset

Third, again if this is SQL, "$genderseek==$Ugender" should read "$Ugender==$genderseek" since judging from your code, $Ugender holds the column name for gender whereas $genderseek is what the user is searching for?

Finally, you should really look into prepared statements. Your current code seems to be very vulnerable to SQL injections!

Upvotes: 3

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22094

You would need to use PHP strtolower() function to compare everything at lower case. This way, case sensitivity will be eliminated.

Upvotes: 0

Related Questions