Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php

What is the strcoll() function in PHP?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 $string1 value is greater than the $string2 value

  • A value less than 0: This is when the $string1 value is less than the $string2 value

  • A 0 value: This will be the return value when $string1 and $string2 are 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 to strcmp()(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 > 0
echo strcoll($string1B , $string2B) ."\n"; //output value = 0
echo strcoll($string1C,$string2C); // //output value < 0
?>

Explanation

  • Line 4–11: We declare variables.

  • Line 15: We compare values with $string1 greater than $string2.

  • Line 16: We compare values with $string1 equal to $string2.

  • Line 17: We compare values with $string1 less than $string2.

RELATED TAGS

php

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring