php.net |  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
Votes:13
Avg. Score:3.7 ± 1.4
Reproduced:7 of 9 (77.8%)
Same Version:3 (42.9%)
Same OS:1 (14.3%)
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: charlie at charliesomerville dot com
New email:
PHP Version: OS:

 

 [2011-02-12 08:09 UTC] charlie at charliesomerville dot com
Description:
------------
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:
---------------
<?php

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

Expected result:
----------------
1

Actual result:
--------------
4

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-12 08:32 UTC] kalle@php.net
-Status: Open +Status: Wont fix
 [2011-02-12 08:32 UTC] kalle@php.net
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: Fri Mar 29 00:01:28 2024 UTC