PHP function strcasecmp() – Binary safe case-insensitive string comparison

The strcasecmp() function is used to compare two case-sensitive strings. This is a case-insensitive version of the strcmp(). This function was introduced in PHP3. The function returns a number less than 0 if one is less than two, 0 if the two strings are equal, and a number greater than 0 if one is greater than two. The comparison is case-insensitive—that is, “Alphabet” and “alphabet” are considered equal.

Syntax:

strcasecmp(string $string1, string $string2): int

Consider the example shown below:

$result = strcasecmp("Hello", "hello");

That will return 0, because PHP will ignore the case difference. Using strcmp() instead would have returned -1: “Hello” would come before “hello”.

Parameters

Parameter Description
string1 The first string
string2 The second string

Return Values

Returns 0 if string1 is greater than string2, and 0 if they are equal.

Examples

Example 1:

<?php
$text1 = “Good morning”;
$text2 = “Good morning”;
if (strcasecmp($text1, $text2) == 0) {
echo ‘$text1 is equal to $text2 in a case-insen-
sitive string comparison’; }
?>

The output of the above program is as follows:

$text1 is equal to $text2 in a case-insensitive string comparison
Related Post