PHP 中的 == 是区分大小写的字符串比较吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3506706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is == in PHP a case-sensitive string comparison?
提问by Colin Pickard
I was unable to find this on php.net. Is the double equal sign (==
) case sensitive when used to compare strings in PHP?
我无法在 php.net 上找到它。==
用于比较 PHP 中的字符串时,双等号 ( ) 是否区分大小写?
回答by Colin Pickard
Yes, ==
is case sensitive.
是的,==
区分大小写。
You can use strcasecmp
for case insensitive comparison
您可以strcasecmp
用于不区分大小写的比较
回答by 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.
如果您正在比较 unicode 字符串,您可能希望先对它们进行规范化。看Normalizer
课。
Example (output in UTF-8):
示例(以 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
回答by Stephen
Yes, ==
is case sensitive.
是的,==
区分大小写。
Incidentally, for a non case sensitive compare, use strcasecmp
:
顺便说一句,对于不区分大小写的比较,请使用strcasecmp
:
<?php
$var1 = "Hello";
$var2 = "hello";
echo (strcasecmp($var1, $var2) == 0); // TRUE;
?>
回答by Frxstrem
==
is case-sensitive, yes.
==
是区分大小写的,是的。
To compare strings insensitively, you can use either strtolower($x) == strtolower($y)
or strcasecmp($x, $y) == 0
要不敏感地比较字符串,您可以使用strtolower($x) == strtolower($y)
或strcasecmp($x, $y) == 0
回答by Robert
==
is case sensitive, some other operands from the php manual to familiarize yourself with
==
区分大小写,php手册中的一些其他操作数以熟悉
http://www.php.net/manual/en/language.operators.comparison.php
http://www.php.net/manual/en/language.operators.comparison.php
回答by Salvi Pascual
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!
我希望它有效!
回答by Site Antipas
You could try comparing with a hash function instead
您可以尝试与哈希函数进行比较
if( md5('string1') == md5('string2') ) {
// strings are equal
}else {
// strings are not equal
}