Home » PHP strcoll() function

PHP strcoll() function

by Online Tutorials Library

PHP string strcoll() Function

strcoll() is an inbuilt string function of PHP, which is used to compare two strings. It is a locale based string comparison.

It is important to notice that the comparison done by strcoll() function is case-sensitive as strcmp(), which means it treats capital and small case differently during the comparison. But, it is not binary safe like strcmp() function.

Note: strcoll() function is case sensitive and it is not binary safe unlike strcmp() function.

Syntax:

Parameters

This function takes two strings which are mandatory to pass into the parameter. Description of the following parameters is given below.

  1. $str1 (mandatory) – It is the first parameter of this function that is used in comparison.
  2. $str2 (mandatory) – It is the second parameter of this function to be used in comparison.

Both parameters are mandatory to pass into the function.

Value returns by strcoll()

Strcoll() returns a random integer value which depends on matching condition.

Return 0 – It returns 0 if both strings are equal, i.e., $str1 = $str2

Return < 0 – It returns negative value (<0), if first string is lesser than second string, i.e., $str1 < $str2

Return > 0 – It will return positive value (>0), if first string is greater than second string, i.e., $str1 > $str2

Note: It calculates ASCII value of the string and then compares both strings to check whether they are equal, greater or less from each other.

Example 1

Output:

0 because both strings are equal.   1 because the first string is greater than the second string.  

Example 2

Output:

-1 because the first string is less than the second string.  1 because the first string is greater than the second string.  

Example 3

Output:

1 because the first string is greater than the second string.  -1  because the first string is less than the second string.  

There is a table given below which contains some easy and simple example with their explanation of strcoll() function to understand it more quickly.

String1 String2 Output Explanation
tutoraspire tutoraspire 0 Both strings are same and equal.
tutoraspire TUTORASPIRE 1 String1 > String2 because ASCII value of J is 74 and j is 106 so that j > J. It treats small and capital letters differently.
TUTORASPIRE tutoraspire -1 String1 < String2 because ASCII value of J is 74 and j is 106 so that J < j.
tutorAspire tutoraspire -1 String1 < String2 because ASCII value of T is 84 and t is 116 so that T < t.
Java Java 1 String1 > String2
Tutoraspire java -1 String1 < String2 because ASCII value of J is 74, and j is 106, so that J < j. Here it will not check string length.

Next TopicPHP String

You may also like