The `strcmp()` function in PHP is used to compare two strings binary-safely (case-sensitive). It returns:This is how PHP 8. PHP 8.1, PHP 8.2, PHP 8.3,and PHP 8.4.1. 0 if the strings are equal2. <0 if string1 is less than string23. >0 if string1 is greater than string2
Example in PHP 8.2<?php$string1 = "apple";$string2 = "banana";$string3 = "Apple";$string4 = "apple";// Comparing different strings$result1 = strcmp($string1, $string2);echo "Comparing '$string1' and '$string2': $result1\n"; // Output will be negative// Comparing same strings with different case$result2 = strcmp($string1, $string3);echo "Comparing '$string1' and '$string3': $result2\n"; // Output will be positive (ASCII comparison)// Comparing identical strings$result3 = strcmp($string1, $string4);echo "Comparing '$string1' and '$string4': $result3\n"; // Output will be 0// Using in conditional statementsif (strcmp($string1, $string4) === 0) {echo "'$string1' and '$string4' are identical\n";} else {echo "'$string1' and '$string4' are different\n";}?>Output:1.Comparing 'apple' and 'banana': -12.Comparing 'apple' and 'Apple': 323.Comparing 'apple' and 'apple': 04.apple' and 'apple' are identical
Notes:1. `strcmp()` is case-sensitive - use `strcasecmp()` for case-insensitive comparison2. In PHP 8.2, this function works the same way as in previous versions3. The function performs binary-safe string comparison4. For simple equality checks, you might prefer `===` operator, but `strcmp()` is useful for sorting or when you need to know the ordering of strings.