php 使用“==”与“strcmp()”的字符串比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3333353/
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
String comparison using '==' vs. 'strcmp()'
提问by Jiew Meng
It seems that PHP's ===operator is case sensitive. So is there a reason to use strcmp()?
PHP 的===操作符似乎是区分大小写的。那么有理由使用strcmp()吗?
Is it safe to do something like the following?
执行以下操作是否安全?
if ($password === $password2) { ... }
采纳答案by deceze
The reason to use it is because strcmp
使用它的原因是因为 strcmp
returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
如果 str1 小于 str2,则返回 < 0;> 0 如果 str1 大于 str2,如果它们相等则为 0。
===only returns trueor false, it doesn't tell you which is the "greater" string.
===只返回trueor false,它不会告诉您哪个是“更大”的字符串。
回答by postfuturist
You should never use ==for string comparison. ===is OK.
你永远不应该==用于字符串比较。===没问题。
$something = 0;
echo ('password123' == $something) ? 'true' : 'false';
Just run the above code and you'll see why.
只需运行上面的代码,你就会明白为什么。
$something = 0;
echo ('password123' === $something) ? 'true' : 'false';
Now, that's a little better.
现在,那好一点。
回答by Antimony
Don't use ==in PHP. It will not do what you expect. Even if you are comparing strings to strings, PHP will implicitly cast them to floats and do a numerical comparison if they appear numerical.
不要==在 PHP 中使用。它不会做你所期望的。即使您将字符串与字符串进行比较,PHP 也会隐式地将它们转换为浮点数,并在它们出现数字时进行数字比较。
For example '1e3' == '1000'returns true. You should use ===instead.
例如'1e3' == '1000'返回true。你应该===改用。
回答by Ajith
Well..according to this php bug report, you can even get 0wned.
好吧..根据这个php 错误报告,你甚至可以得到 0wned。
<?php
$pass = isset($_GET['pass']) ? $_GET['pass'] : '';
// Query /?pass[]= will authorize user
//strcmp and strcasecmp both are prone to this hack
if ( strcasecmp( $pass, '123456' ) == 0 ){
echo 'You successfully logged in.';
}
?>
It gives you a warning , but still bypass the comparison.
You should be doing ===as @postfuturist suggested.
它给你一个警告,但仍然绕过比较。
你应该===按照@postfuturist 的建议去做。
回答by DataPriest
Always remember, when comparing strings, you should use ===operator (strict comparison) and not==operator (loose comparison).
永远记住,在比较字符串时,应该使用===运算符(严格比较)而不是==运算符(松散比较)。
回答by Balmipour
Summing up all answers :
总结所有答案:
==is a bad ideafor string comparisons.
It will give you "surprising" results in many cases. Don't trust it.===is fine, and will give you the best performance.strcmp()should be used if you need to determine which string is "greater", typically for sorting operations.
==对于字符串比较来说是个坏主意。
在许多情况下,它会给你“令人惊讶”的结果。不要相信它。===很好,会给你最好的表现。strcmp()如果您需要确定哪个字符串“更大”,则应该使用它,通常用于排序操作。
回答by Nikola Petkanski
Using ==might be dangerous.
使用==可能是危险的。
Note, that it would cast the variable to another data type if the two differs.
请注意,如果两者不同,它会将变量转换为另一种数据类型。
Examples:
例子:
echo (1 == '1') ? 'true' : 'false';echo (1 == true) ? 'true' : 'false';
echo (1 == '1') ? 'true' : 'false';echo (1 == true) ? 'true' : 'false';
As you can see, these two are from different types, but the result is true, which might not be what your code will expect.
如您所见,这两个来自不同的类型,但结果是true,这可能不是您的代码所期望的。
Using ===, however, is recommended as test shows that it's a bit faster than strcmp()and its case-insensitive alternative strcasecmp().
===但是,建议使用,因为测试表明它比strcmp()及其不区分大小写的替代方案快一点strcasecmp()。
Quick googling yells this speed comparison: http://snipplr.com/view/758/
快速谷歌搜索大喊这个速度比较:http: //snipplr.com/view/758/
回答by ungalcrys
strcmp()and ===are both case sensitive but ===is much faster
strcmp()并且===都区分大小写,但===速度要快得多
sample code: http://snipplr.com/view/758/
示例代码:http: //snipplr.com/view/758/
回答by kta
strcmp will return different values based on the environment it is running(Linux/Windows)!
strcmp 将根据它运行的环境(Linux/Windows)返回不同的值!
The reason is the that it has a bug as the bug report says https://bugs.php.net/bug.php?id=53999
原因是它有一个错误,因为错误报告说 https://bugs.php.net/bug.php?id=53999
Please handle with care!!Thank you.
请小心处理!!谢谢。
回答by Daniel Egeberg
You can use strcmp()if you wish to order/compare strings lexicographically. If you just wish to check for equality then ==is just fine.
strcmp()如果您希望按字典顺序排序/比较字符串,则可以使用。如果您只想检查相等性,那==就没问题了。

