为什么两个字符串看起来相同,但它们不是?

时间:2011-11-23 13:27:31

标签: php string-comparison

我的PHP代码:

$hello = "Hello, world! ";
echo $string1 = sprintf("%'#-20s\n", $hello); // Displays "Hello, world! ######"
echo $string2 = str_pad($hello, 20, "#");     // Displays "Hello, world! ######"
echo ($string1 == $string2) ? "Indeed they're equal" : "They're not equal";
                                              //  Displays "They're not equal"
echo strcmp($string1, $string2); // Displays "-1", which (according to PHP Manual)
                                 // means that $string1 is less than $string2

字符串$string1& $string2不平等?

1 个答案:

答案 0 :(得分:7)

因为第一个\n中有一个新行sprintf

相关问题