Почему две строки выглядят одинаковыми, а они нет? - PullRequest
1 голос
/ 23 ноября 2011

Мой код 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 Ответ

7 голосов
/ 23 ноября 2011

потому что есть новая строка \n в первом sprintf.

...