Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Is PHP string comparison case-sensitive?

Tip: The strcasecmp() function is binary-safe and case-insensitive. Tip: This function is similar to the strncasecmp() function, with the difference that you can specify the number of characters from each string to be used in the comparison with strncasecmp().

Can you use == to compare strings in PHP?

The most common way you will see of comparing two strings is simply by using the == operator if the two strings are equal to each other then it returns true. This code will return that the strings match, but what if the strings were not in the same case it will not match.

How can compare string without case-sensitive in PHP?

The strcasecmp() function is a built-in function in PHP and is used to compare two given strings. It is case-insensitive. This function is similar to strncasecmp(), the only difference is that the strncasecmp() provides the provision to specify the number of characters to be used from each string for the comparison.

What is case-sensitive string comparison?

You can compare two strings using either equals() method or compareTo() method. Where, The equals() method compares this string to the specified object.


Yes, == is case sensitive.

You can use strcasecmp for case insensitive comparison


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

Yes, == is case sensitive.

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

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

== is case-sensitive, yes.

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

  • strtolower()
  • strcasecmp()

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

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