how to compare case insensitive two strings in php

前端 未结 2 652
粉色の甜心
粉色の甜心 2021-01-26 16:19

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

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


        
相关标签:
2条回答
  • 2021-01-26 16:36

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

    0 讨论(0)
  • 2021-01-26 16:40

    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!

    0 讨论(0)
提交回复
热议问题