|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53999 strcmp() doesn't always return -1, 0, or 1
Submitted: 2011-02-12 08:09 UTC Modified: 2011-02-12 08:35 UTC
Avg. Score:3.8 ± 1.3
Reproduced:6 of 8 (75.0%)
Same Version:3 (50.0%)
Same OS:1 (16.7%)
From: charlie at charliesomerville dot com Assigned:
Status: Wont fix Package: SPL related
PHP Version: Irrelevant OS: Mac OS X
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
48 - 37 = ?
Subscribe to this entry?

 [2011-02-12 08:09 UTC] charlie at charliesomerville dot com
PHP's strcmp() return value is inconsistent across operating systems.

On Windows and Linux, strcmp("foo","bar") returns 1. However, on Mac OS X, 
strcmp("foo","bar") returns 4.

Technically, both of these return values are correct, however the 4 is 
inconsistent with the return values on other operating systems.

Test script:

echo strcmp("foo","bar");

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-12 08:32 UTC]
-Status: Open +Status: Wont fix
 [2011-02-12 08:32 UTC]
The documentation for strcmp() does not state it returns -1, 0 or 1:

"Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal."

The memcmp() man page also states that (which is what strcmp internally uses), so if you want the same cross platform behaviour you should alter your script to do as specified above.
 [2011-02-12 08:35 UTC] charlie at charliesomerville dot com
While I know that all the documentation promises is < 0, == 0 and > 0, as PHP is 
supposed to provide an OS-agnostic environment, shouldn't strcmp() return 
consistent values across the board?
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Feb 29 14:01:34 2024 UTC