2

I have this code:

$a = 'abc';
$b = 'AbC';

if ($a == $b)
{
    echo 'abc == ABc!';
}
else
{
    echo 'abc != ABc!';
}

Now it echoes abc != ABc! but i'd like it to match the strings regardless of the capitals.

CodingMage
  • 135
  • 1
  • 15

1 Answers1

3

Two options:

1) convert the casing and do a comparison.

strtolower($a) === strtolower($b)

One caveat of this is that for non-utf8 characters and non-english languages this does not work well.

2) use case insensitive comparison

if (strcasecmp($a, $b) == 0) {

strcasecmp docs

Ne Ma
  • 1,719
  • 13
  • 19
  • Thanks! `strcasecmp` works perfectly! I checked out `strtolower` earlier but it wouldn't convert Ä to ä, which i kinda needed. – CodingMage Jul 30 '18 at 09:52
  • I did note that its not good for non-utf8 but forgot about non-english languages. Thanks for the feedback - I have amended my answer with that information in case someone comes across it in the future. – Ne Ma Jul 30 '18 at 10:00