Bohdan Zhuravel
Bohdan Zhuravel

Reputation: 145

Why are two strings look equal but they're not?

My PHP code:

$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

Any reason why the strings $string1 & $string2 aren't equal?

Upvotes: 1

Views: 344

Answers (1)

ajreal
ajreal

Reputation: 47321

because there is a new line \n in the first sprintf.

Upvotes: 7

Related Questions