136 of 410 menu

The strcasecmp Function

The strcasecmp function performs a binary-safe case-insensitive string comparison. The first parameter is the first string to compare, the second is the second string. The function returns 0 if the strings are identical, a number -1 if the first string is less than the second, and a number greater than 1 if the first string is greater than the second.

Syntax

strcasecmp(string $str1, string $str2): int

Example

Comparing two identical strings in different cases:

<?php $res = strcasecmp('Hello', 'hello'); echo $res; ?>

Code execution result:

0

Example

Comparing strings where the first is less than the second:

<?php $res = strcasecmp('apple', 'banana'); echo $res; ?>

Code execution result:

-1

Example

Comparing strings where the first is greater than the second:

<?php $res = strcasecmp('zebra', 'apple'); echo $res; ?>

Code execution result:

1

See Also

  • the strcmp function,
    which compares strings case-sensitively
  • the strncasecmp function,
    which compares the first n characters of strings case-insensitively
byenru