What is the strcoll() function in PHP?
Overview
The strcoll() method is built into PHP and is available for PHP version 4 and later. It is a method that takes in two strings and makes a case-sensitive comparison between them. It is not a binary save. This is because we cannot pass it an arbitrary binary value as this function does not cleanly separate strings and binary data.
Syntax
strcoll($string1, string $string2)
Parameters
$string1: This is the first string that will be compared.$string: This is the second string that will be compared with another in a case-sensitive comparison operation.
The value returned by this function after a successful case sensitive string comparison can be any of the following:
-
A value greater than 0: This value will be returned by the function when the
$string1value is greater than the$string2value -
A value less than 0: This is when the
$string1value is less than the$string2value -
A 0 value: This will be the return value when
$string1and$string2are equal.
strcoll()uses the current locale (default language for non-Unicode programs) for these comparisons. In a situation where the current locale is set as C or POSIX, this function can be seen as the equivalent tostrcmp()(another case-sensitive comparison function).
Code
<?php//declare some variables$string1A = "This is a good day";$string2A = "This is a GOOD day";$string1B = "Educative Edpresso";$string2B = "Educative Edpresso";$string1C = "WE CAN DO IT TOGETHER";$string2C = "We Can Do It Together";//use the strcoll() function and display the result.echo strcoll($string1A,$string2A ) ."\n"; //output value > 0echo strcoll($string1B , $string2B) ."\n"; //output value = 0echo strcoll($string1C,$string2C); // //output value < 0?>
Explanation
-
Line 4–11: We declare variables.
-
Line 15: We compare values with
$string1greater than$string2. -
Line 16: We compare values with
$string1equal to$string2. -
Line 17: We compare values with
$string1less than$string2.