Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

strcasecmp

Compares two strings, ignoring case.

Syntax

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

Parameters

ParameterTypeDescription
$string1stringThe first string.
$string2stringThe second string.

Return value

int`0` if the strings are equal; a negative or positive number if the first is smaller or greater.

Examples

Equality regardless of case:

<?php
 
var_dump(strcasecmp('Hello', 'HELLO') === 0);

Output:

bool(true)

Sorting regardless of case:

<?php
 
$names = ['bob', 'Anna', 'carl'];
usort($names, 'strcasecmp');
 
echo implode(', ', $names);

Output:

Anna, bob, carl

Notes

Case is ignored for ASCII letters only. For other alphabets compare the results of mb_strtolower().

See also

  • strcmp — Compares two strings.
  • strnatcmp — Compares strings in "natural" order — numbers inside strings as numbers.
  • strtolower — Converts ASCII letters to lowercase.

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.