strcasecmp / strncasecmp
strcasecmp(string $string1, string $string2): int — strncasecmp(string $string1, string $string2, int $length): int
Binary-safe case-insensitive comparison: same sign semantics as strcmp/strncmp, ASCII-only folding (locale-independent since 8.2); only the sign of the result is portable.
C C23
/* strcasecmp(3) / strncasecmp(3): POSIX case-insensitive comparison
(<strings.h>; Windows maps to _stricmp/_strnicmp). Only the SIGN
of the result is portable - same convention as strcmp. */
#include <stdio.h>
#include <strings.h>
int main(void) {
printf("%d\n", strcasecmp("Hello", "hello") == 0);
printf("%d\n", strcasecmp("apple", "apricot") < 0);
printf("%d\n", strncasecmp("photo.jpg", "photo.jpeg", 8) == 0);
printf("%d\n", strncasecmp("photo.jpg", "photo.jpeg", 9) > 0);
return 0;
}
1 1 1 1
C++ C++26
// Same family as C: strcasecmp/strncasecmp from <strings.h> (POSIX).
// A pure-C++ alternative compares lowercase copies - shown for the last check.
#include <cctype>
#include <cstdio>
#include <string>
#include <strings.h>
static bool lower_eq(const std::string& a, const std::string& b) { // ASCII fold helper
if (a.size() != b.size()) return false;
for (size_t i = 0; i < a.size(); ++i)
if (std::tolower((unsigned char)a[i]) != std::tolower((unsigned char)b[i])) return false;
return true;
}
int main() {
printf("%d\n", strcasecmp("Hello", "hello") == 0);
printf("%d\n", strcasecmp("apple", "apricot") < 0);
printf("%d\n", strncasecmp("photo.jpg", "photo.jpeg", 8) == 0);
printf("%d\n", strncasecmp("photo.jpg", "photo.jpeg", 9) > 0);
printf("%d\n", lower_eq("HELLO", "hello"));
}
1 1 1 1 1
C# v11.0.100
// string.Compare with StringComparison.OrdinalIgnoreCase: the strcasecmp
// equivalent; there is no strncasecmp - compare substrings for the prefix form.
Console.WriteLine(string.Compare("Hello", "hello", StringComparison.OrdinalIgnoreCase) == 0 ? "eq" : "ne");
Console.WriteLine(string.Compare("apple", "apricot", StringComparison.OrdinalIgnoreCase) < 0 ? "lt" : "ge");
Console.WriteLine(string.Compare("photo.jpg", 0, "photo.jpeg", 0, 8, StringComparison.OrdinalIgnoreCase) == 0 ? "prefix8 eq" : "prefix8 ne");
Console.WriteLine(string.Compare("photo.jpg", 0, "photo.jpeg", 0, 9, StringComparison.OrdinalIgnoreCase) > 0 ? "prefix9 gt" : "prefix9 le");
eq lt prefix8 eq prefix9 gt
Go v1.27.4
// strings.EqualFold is the case-insensitive equality (simple Unicode folding,
// ASCII range matches strcasecmp); prefix comparison folds two slices.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.EqualFold("Hello", "hello"))
fmt.Println(strings.Compare(strings.ToLower("apple"), strings.ToLower("apricot")) < 0)
fmt.Println(strings.EqualFold("photo.jpg"[:8], "photo.jpeg"[:8]))
fmt.Println(strings.Compare(strings.ToLower("photo.jpg"[:9]), strings.ToLower("photo.jpeg"[:9])) > 0)
}
true true true true
Java v27.0.1
// compareToIgnoreCase is the strcasecmp equivalent (sign only is portable);
// regionMatches(true, ...) is the strncasecmp prefix form.
public class Main {
public static void main(String[] args) {
System.out.println("Hello".compareToIgnoreCase("hello") == 0 ? "eq" : "ne");
System.out.println("apple".compareToIgnoreCase("apricot") < 0 ? "lt" : "ge");
System.out.println("photo.jpg".regionMatches(true, 0, "photo.jpeg", 0, 8) ? "prefix8 eq" : "prefix8 ne");
System.out.println("photo.jpg".regionMatches(true, 0, "photo.jpeg", 0, 9) ? "prefix9 eq" : "prefix9 ne");
}
}
eq lt prefix8 eq prefix9 ne
JavaScript v26.13.1
// No built-in case-insensitive compare; the idiom lowercases both sides.
// ASCII-only folding matches strcasecmp semantics.
console.log("Hello".toLowerCase() === "hello".toLowerCase());
console.log("apple".toLowerCase() < "apricot".toLowerCase());
console.log("photo.jpg".slice(0, 8).toLowerCase() === "photo.jpeg".slice(0, 8).toLowerCase());
console.log("photo.jpg".slice(0, 9).toLowerCase() > "photo.jpeg".slice(0, 9).toLowerCase());
true true true true
PHP v8.6.0
// strcasecmp / strncasecmp - binary-safe CASE-INSENSITIVE comparison.
// Only the SIGN of the result is portable (PHP 8.2+); folding is ASCII-only
// and locale-independent since 8.2.
var_dump(strcasecmp("Hello", "hello")); // equal ignoring case -> 0
var_dump(strcasecmp("apple", "apricot") < 0); // sign: apple sorts before apricot
var_dump(strncasecmp("photo.jpg", "photo.jpeg", 8) === 0); // first 8 chars fold equal
var_dump(strncasecmp("photo.jpg", "photo.jpeg", 9) > 0); // char 9: 'g' sorts after 'e'
var_dump(strcasecmp("HELLO", "hello")); // uppercase vs lowercase -> 0
int(0) bool(true) bool(true) bool(true) int(0)
Python v3.15.2
# No case-insensitive compare primitive; lowercase (or casefold) both sides.
print("Hello".lower() == "hello".lower())
print("apple".lower() < "apricot".lower())
print("photo.jpg"[:8].lower() == "photo.jpeg"[:8].lower())
print("photo.jpg"[:9].lower() > "photo.jpeg"[:9].lower())
True True True True
Rust v1.99.1
// eq_ignore_ascii_case is the strcasecmp equality twin; for ordering and
// prefixes, compare lowercase forms (only the sign is portable).
fn main() {
println!("{}", "Hello".eq_ignore_ascii_case("hello"));
println!("{}", "apple".to_ascii_lowercase() < "apricot".to_ascii_lowercase());
println!("{}", "photo.jpg".get(..8).unwrap().eq_ignore_ascii_case("photo.jpeg".get(..8).unwrap()));
println!("{}", "photo.jpg".get(..9).unwrap().to_ascii_lowercase() > "photo.jpeg".get(..9).unwrap().to_ascii_lowercase());
}
true true true true
TypeScript v26.13.1
// Same as JavaScript: lowercase both sides, then compare.
console.log("Hello".toLowerCase() === "hello".toLowerCase());
console.log("apple".toLowerCase() < "apricot".toLowerCase());
console.log("photo.jpg".slice(0, 8).toLowerCase() === "photo.jpeg".slice(0, 8).toLowerCase());
console.log("photo.jpg".slice(0, 9).toLowerCase() > "photo.jpeg".slice(0, 9).toLowerCase());
true true true true
Language differences
C: strcasecmp/strncasecmp are the POSIX same-family functions (<strings.h>; Windows maps _stricmp/_strnicmp) - PHP 8.2+ aligns verbatim with their "ASCII-only folding + sign semantics". C++: same family, plus a hand-written ASCII-fold helper showing the pure-C++ route. C#: string.Compare with OrdinalIgnoreCase; the Ordinal suffix means locale-free (InvariantCultureIgnoreCase is the near-equivalent alternative). Go: strings.EqualFold is simple Unicode folding - identical to strcasecmp for ASCII, but non-ASCII cases (Turkish dotted I etc.) need care on both sides. Java: compareToIgnoreCase returns the sign; regionMatches(true, ...) is the strncasecmp prefix form (returns bool only). JavaScript/TypeScript: the idiom a.toLowerCase() === b.toLowerCase() - toLowerCase folds more Unicode than PHP does. PHP: mirrors strcmp/strncmp (see the strcmp_strncmp entry): same names minus "case", semantics minus case; since 8.2 folding no longer depends on setlocale() - ASCII only, non-ASCII bytes compared by byte value; only the SIGN is portable (8.2+ may return -1/1), never depend on the magnitude; the multibyte-safe alternative is a hand-written mb_strcasecmp (strcmp(mb_strtoupper(a), mb_strtoupper(b))). Python: lower() idiom (casefold() is stronger Unicode folding); like JS, folds wider than PHP. Rust: eq_ignore_ascii_case aligns verbatim with PHP ASCII semantics; ordering compares lowercased forms. Teaching anchor: three folding scopes - ASCII-only (PHP/C/Rust), language-aware Unicode folding (Python casefold / JS toLowerCase), locale-aware (pre-8.2 PHP trap, since removed).