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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare variable and initialize it
$str1 = 'Geeks';
$str2 = 'geeks';
  
$coll = collator_create( 'en_US' );
  
// Compare both strings
$res = collator_compare( $coll, $str1, $str2 );
  
if ($res === false)
    echo collator_get_error_message( $coll );
else if( $res > 0 )
    echo $str1 . " is greater than " . $str2 . "\n";
else if( $res < 0 )
    echo $str1 . " is less than " . $str2 . "\n";
else
    echo $str1 . " is equal to " . $str2;
?>

chevron_right


Output:

Geeks is greater than geeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare the variable and initialize it
$str1 = 'GeeksforGeeks';
$str2 = 'GeeksforGeeks';
  
$coll = collator_create( 'en_US' );
  
// Compare both strings
$res  = collator_compare( $coll, $str1, $str2 );
  
if ($res === false)
    echo collator_get_error_message( $coll );
else if( $res > 0 )
    echo $str1 . " is greater than " . $str2 . "\n";
else if( $res < 0 )
    echo $str1 . " is less than " . $str2 . "\n";
else
    echo $str1 . " is equal to " . $str2;
?>

chevron_right


Output:

GeeksforGeeks is equal to GeeksforGeeks

Reference: http://php.net/manual/en/collator.compare.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.