93

I was unable to find this on php.net. Is the double equal sign (==) case sensitive when used to compare strings in PHP?

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175

7 Answers7

107

Yes, == is case sensitive.

You can use strcasecmp for case insensitive comparison

Player1
  • 2,878
  • 2
  • 26
  • 38
Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
21

Yes, but it does a comparison byte-by-byte.

If you're comparing unicode strings, you may wish to normalize them first. See the Normalizer class.

Example (output in UTF-8):

$s1 = mb_convert_encoding("\x00\xe9", "UTF-8", "UTF-16BE");
$s2 = mb_convert_encoding("\x00\x65\x03\x01", "UTF-8", "UTF-16BE");
//look the same:
echo $s1, "\n";
echo $s2, "\n";
var_dump($s1 == $s2); //false
var_dump(Normalizer::normalize($s1) == Normalizer::normalize($s2)); //true
Huseyin Yagli
  • 9,896
  • 6
  • 41
  • 50
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • 4
    +1 for insight that it's not really string comparison (it's binary comparison). Hence it's technically not case-sensitive (Although in 99.999% of cases it behaves just like it)... – ircmaxell Aug 17 '10 at 20:44
12

Yes, == is case sensitive.

Incidentally, for a non case sensitive compare, use strcasecmp:

<?php
    $var1 = "Hello";
    $var2 = "hello";
    echo (strcasecmp($var1, $var2) == 0); // TRUE;
?>
Player1
  • 2,878
  • 2
  • 26
  • 38
Stephen
  • 6,027
  • 4
  • 37
  • 55
9

== is case-sensitive, yes.

To compare strings insensitively, you can use either strtolower($x) == strtolower($y) or strcasecmp($x, $y) == 0

Frxstrem
  • 38,761
  • 9
  • 79
  • 119
2

== is case sensitive, some other operands from the php manual to familiarize yourself with

http://www.php.net/manual/en/language.operators.comparison.php

Robert
  • 21,110
  • 9
  • 55
  • 65
1

Yes, == is case sensitive. The easiest way for me is to convert to uppercase and then compare. In instance:

$var = "Hello";
if(strtoupper($var) == "HELLO") {
    echo "identical";
}
else {
    echo "non identical";
}

I hope it works!

Salvi Pascual
  • 1,788
  • 17
  • 22
1

You could try comparing with a hash function instead

  if( md5('string1') == md5('string2') ) {
    // strings are equal
  }else {
    // strings are not equal
  }
Site Antipas
  • 141
  • 1
  • 4