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

巧了我就是萌 提交于 2019-11-28 18:04:34
Colin Pickard

Yes, == is case sensitive.

You can use strcasecmp for case insensitive comparison

Artefacto

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
Stephen

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

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

http://www.php.net/manual/en/language.operators.comparison.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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!