Is == in PHP a case-sensitive string comparison?

PhpString Comparison

Php Problem Overview


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

Php Solutions


Solution 1 - Php

Yes, == is case sensitive.

You can use strcasecmp for case insensitive comparison

Solution 2 - Php

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

Solution 3 - Php

Yes, == is case sensitive.

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

<?php
    $var1 = "Hello";
    $var2 = "hello";
    echo (strcasecmp($var1, $var2) == 0); // TRUE;
?>

Solution 4 - Php

== is case-sensitive, yes.

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

Solution 5 - Php

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

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

Solution 6 - Php

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!

Solution 7 - Php

You could try comparing with a hash function instead

  if( md5('string1') == md5('string2') ) {
    // strings are equal
  }else {
    // strings are not equal
  }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser374343View Question on Stackoverflow
Solution 1 - PhpColin PickardView Answer on Stackoverflow
Solution 2 - PhpArtefactoView Answer on Stackoverflow
Solution 3 - PhpStephenView Answer on Stackoverflow
Solution 4 - PhpFrxstremView Answer on Stackoverflow
Solution 5 - PhpRobertView Answer on Stackoverflow
Solution 6 - PhpSalvi PascualView Answer on Stackoverflow
Solution 7 - PhpSite AntipasView Answer on Stackoverflow