Open In App

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:



Parameters: This function accepts three parameter as mentioned above and described below:

Return Value: This function returns the comparison result which are given below:



Below programs illustrate the collator_compare() function in PHP:

Program 1:




<?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;
?>

Output:
Geeks is greater than geeks

Program 2:




<?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;
?>

Output:
GeeksforGeeks is equal to GeeksforGeeks

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


Article Tags :