strcasecmp
Compares two strings, ignoring case.
Syntax
strcasecmp(string $string1, string $string2): intParameters
| Parameter | Type | Description |
|---|---|---|
| $string1 | string | The first string. |
| $string2 | string | The 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, carlNotes
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.