Search code examples
phpif-statementcase-insensitive

Case insensitive string comparison


I would like to compare two variables to see if they are the same, but I want this comparison to be case-insensitive.

For example, this would be case sensitive:

if($var1 == $var2){
   ...
}

But I want this to be case insensitive, how would I approach this?


Solution

  • This is fairly simple; you just need to call strtolower() on both variables.

    If you need to deal with Unicode or international character sets, you can use mb_strtolower().

    Please note that other answers suggest using strcasecmp()—that function does not handle multibyte characters, so results for any UTF-8 string will be bogus.