strcmp() is fully supported in both PHP 8.4 and PHP 8.5, with no changes to its core behavior in these versions. The function remains a binary-safe, case sensitive string comparison tool.
Function & Return Values
The signature is strcmp(string $string1, string $string2): int.
It returns:
- < 0 if string1 is less than string2
- > 0 if string1 is greater than string2
- 0 if they are equal
Important: Do not rely on specific return values (like -1 or 1). The official documentation states that "No particular meaning can be reliably inferred from the value aside from its sign" . You should only check if the result is negative, positive, or zero.
PHP 8.4/8.5 Status
- PHP 8.4: Supported. The official string changelog for PHP 8.4 does not list strcmp as having any deprecated or changed behaviour.
- PHP 8.5: No deprecation or removal has been announced. PHP 8.5 is not yet released, and no official list of deprecated features exists for it.
- Historical Note (PHP 8.2): The only recent significant change was in PHP 8.2.0, where the function is no longer guaranteed to return strlen($string1) - strlen($string2) when lengths differ; it may now return -1 or 1.
Example with Output
<?php
$var1 = "Hello";
$var2 = "hello";// Case-sensitive comparison: H (ASCII 72) is less than h (ASCII 104)
$result = strcmp($var1, $var2);
var_dump($result); // Output: int(-32) or a similar negative value// Equal strings
var_dump(strcmp("test", "test")); // Output: int(0)// Greater than
var_dump(strcmp("z", "a")); // Output: int(1) or a positive value
?>
Notes
- Case-Sensitive: For case-insensitive comparison, use strcasecmp().
- Binary Safe: It compares strings byte by byte, making it safe for arbitrary data but not suitable for multibyte (UTF-8) collation. For locale-aware or Unicode comparisons, use strcoll() or the Collator class.
- Strict Typing: In PHP 8+, passing non-string arguments (like nullor integers) to strcmp may throw a TypeError or produce unpredictable results. Ensure both parameters are strings.