By Rupert


2012-02-15 07:41:55 8 Comments

PHP's documentation on this function is a bit sparse and I have read that this function compares ASCII values so...

echo strcmp('hello', 'hello');
//outputs 0 as expected - strings are equal.
echo '<hr />';

echo strcmp('Hello', 'hello');
//outputs -32, a negative number is expected as 
//uppercase H has a lower ASCII value than lowercase h.
echo '<hr />';

echo strcmp('60', '100');
//outputs 5.

The last example is confusing me. I don't understand why it is outputting a positive number.

  • ASCII Value of 0 = 48
  • ASCII Value of 1 = 49
  • ASCII Value of 6 = 54

  • Total ASCII value of '60' = (54 + 48) = 102

  • Total ASCII value of '100' = (49 + 48 + 48) = 145

The strcmp() functions is saying that '60' is "greater" than '100' even though it seems that the ASCII value and string length of '100' is greater than '60'

Can anyone explain why?

Thanks

3 comments

@wallyk 2012-02-15 07:43:48

strcmp() returns the difference of the first non-matching character between the strings.

6 - 1 is 5.

When you look at it, you are probably not seeing the characters or digits—just the numbers

@Simon Richter 2012-02-15 09:29:15

Note that this is not guaranteed behaviour -- the only thing guaranteed is that the value will be larger than zero for these input parameters.

@wallyk 2012-02-15 17:50:47

@SimonRichter: Agreed it is not guaranteed. But it is traditional. Some code I wrote in 1978 which depends on this method of computing the return value still works!

@Pacerier 2013-07-30 07:30:05

@wallyk, how do you know that this answer is correct when the documentation didn't say that it returns the difference of the first character?

@wallyk 2013-07-30 16:18:19

@Pacerier: I have a lot of experience with a lot of different runtime libraries, from PDP-11s running Unix v6 (the original C), to VAX/VMS, MSDOS, Windows, GCOS, Sequent, IBM, and microcontrollers. They all implement the difference, which, as some CRTL source code indicates, for tradition and perfect compatibility with the original.

@Madara's Ghost 2012-02-15 07:45:03

Because strcmp() stops at the first difference it finds. Hence the difference between the ASCII value of '1' and the ASCII value of '6'

@Pacerier 2013-07-30 07:30:22

@wallyk, how do you know that this behavior is guaranteed when the documentation didn't say that it returns the difference of the first character?

@deceze 2012-02-15 07:45:22

6 is 5 "larger" than 1. This is lexical comparison. The first character is different, that's where the comparison stops.

Related Questions

Sponsored Content

20 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

36 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

13 Answered Questions

[SOLVED] Why shouldn't I use mysql_* functions in PHP?

  • 2012-10-12 13:18:39
  • Madara's Ghost
  • 223514 View
  • 2521 Score
  • 13 Answer
  • Tags:   php mysql

4 Answered Questions

[SOLVED] Is this the only return value for strcmp() in C?

  • 2014-07-29 12:19:35
  • Manish Giri
  • 2367 View
  • 2 Score
  • 4 Answer
  • Tags:   c string strcmp

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

1 Answered Questions

[SOLVED] strcmp output with positive value

  • 2015-09-17 03:23:22
  • SpaceWalker
  • 275 View
  • 0 Score
  • 1 Answer
  • Tags:   c++ strcmp

8 Answered Questions

[SOLVED] Is it possible to change where echo outputs?

  • 2013-02-23 04:44:16
  • user2033561
  • 133 View
  • 2 Score
  • 8 Answer
  • Tags:   php echo

Sponsored Content