PHP | collator_compare() Function

The collator_compare() function is an inbuilt function in PHP which is used to compare two Unicode strings according to collation rules.
Syntax:
- Procedural style:
int collator_compare( $coll, $str1, $str2 ) 
- Object oriented style:
int Collator::compare( $str1, $str2 ) 
Parameters: This function accepts three parameter as mentioned above and described below:
- $coll: This parameter is used as collator object. It provides the comparison capability with support for appropriate locale-sensitive sort orderings.
- $str1: The first string to compare.
- $str2: The second string to compare.
Return Value: This function returns the comparison result which are given below:
- 1: If str1 is greater than str2.
- 0: If str1 is equal to str2.
- -1: If str1 is less than str2.
- Error: It return False.
Below programs illustrate the collator_compare() function in PHP:
Program 1:
| <?php  // Declare variable and initialize it $str1= 'Geeks'; $str2= 'zambiatek';  $coll= collator_create( 'en_US');  // Compare both strings $res= collator_compare( $coll, $str1, $str2);  if($res=== false)     echocollator_get_error_message( $coll); elseif( $res> 0 )     echo$str1. " is greater than ". $str2. "\n"; elseif( $res< 0 )     echo$str1. " is less than ". $str2. "\n"; else    echo$str1. " is equal to ". $str2; ?>  | 
Output:
Geeks is greater than zambiatek
Program 2:
| <?php  // Declare the variable and initialize it $str1= 'zambiatek'; $str2= 'zambiatek';  $coll= collator_create( 'en_US');  // Compare both strings $res= collator_compare( $coll, $str1, $str2);  if($res=== false)     echocollator_get_error_message( $coll); elseif( $res> 0 )     echo$str1. " is greater than ". $str2. "\n"; elseif( $res< 0 )     echo$str1. " is less than ". $str2. "\n"; else    echo$str1. " is equal to ". $str2; ?>  | 
Output:
zambiatek is equal to zambiatek
Reference: http://php.net/manual/en/collator.compare.php
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
 
				 
					


