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

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

Example

Comparing two identical strings in different cases:

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

Code execution result:

php0

Example

Comparing strings where the first is less than the second:

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

Code execution result:

php-1

Example

Comparing strings where the first is greater than the second:

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

Code execution result:

php1

See Also

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