PHP 中的 == 是区分大小写的字符串比较吗?

发布于 2024-09-14 10:13:36 字数 71 浏览 5 评论 0原文

我在 php.net 上找不到这个。在 PHP 中用于比较字符串时,双等号 (==) 是否区分大小写?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

温柔少女心 2024-09-21 10:13:36

是的,== 区分大小写。

您可以使用 strcasecmp 进行不区分大小写的比较

Yes, == is case sensitive.

You can use strcasecmp for case insensitive comparison

相对绾红妆 2024-09-21 10:13:36

是的,但它会逐字节进行比较。

如果您要比较 unicode 字符串,您可能希望首先对它们进行规范化。请参阅 Normalizer 类。

示例(以 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, 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
野却迷人 2024-09-21 10:13:36

是的,== 区分大小写。

顺便说一句,对于不区分大小写的比较,请使用 strcasecmp

<?php
    $var1 = "Hello";
    $var2 = "hello";
    echo (strcasecmp($var1, $var2) == 0); // 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;
?>
寒冷纷飞旳雪 2024-09-21 10:13:36

== 区分大小写,是的。

要不敏感地比较字符串,可以使用 strtolower($x) == strtolower($y)strcasecmp($x, $y) == 0

== is case-sensitive, yes.

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

云柯 2024-09-21 10:13:36

== 区分大小写,一些其他操作数来自 php 手册,以熟悉

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

始终不够 2024-09-21 10:13:36

是的,== 区分大小写。对我来说最简单的方法是转换为大写然后进行比较。例如:

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

我希望它有效!

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!

逐鹿 2024-09-21 10:13:36

您可以尝试与哈希函数进行比较

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

You could try comparing with a hash function instead

  if( md5('string1') == md5('string2') ) {
    // strings are equal
  }else {
    // strings are not equal
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文