W3docs

strncasecmp()

The strncasecmp() function in PHP is used to compare two strings case-insensitively up to a certain length. It is similar to the strncmp() function, but it

Introduction

strncasecmp() compares the first N characters of two strings while ignoring case. It is the bounded, case-insensitive member of PHP's string-comparison family:

  • It is case-insensitive, like strcasecmp()A and a are treated as equal.
  • It only looks at the first $length characters, like strncmp() — the rest of each string is ignored.

In short: strncasecmp() is to strncmp() what strcasecmp() is to strcmp() — same job, but case is folded away before comparing. This page covers the syntax, what the return value actually means, several runnable examples, and the gotchas that bite people in real code.

Syntax

strncasecmp(string $string1, string $string2, int $length): int
ParameterDescription
$string1The first string to compare.
$string2The second string to compare.
$lengthThe maximum number of characters (bytes) to compare from each string.

The function returns an integer, not a boolean — this is the single most common source of bugs (see Gotchas).

What the return value means

The return value is a direction, not a true/false:

Return valueMeaning
< 0 (negative)$string1 sorts before $string2 (within the first $length chars).
0The first $length characters are equal, ignoring case.
> 0 (positive)$string1 sorts after $string2 (within the first $length chars).

Only the sign is contractually guaranteed. Do not rely on the exact magnitude (older PHP returned the difference of the first mismatching byte; PHP 8 may return -1/0/1). Always test with < 0, === 0, or > 0.

Basic example

<?php

$string1 = "Hello World";
$string2 = "hello world";
$length  = 5;

$result = strncasecmp($string1, $string2, $length);

if ($result < 0) {
    echo "The first $length characters of $string1 are less than those of $string2";
} elseif ($result > 0) {
    echo "The first $length characters of $string1 are greater than those of $string2";
} else {
    echo "The first $length characters of $string1 are equal to those of $string2";
}

The first five characters are Hello vs hello. Because the comparison ignores case, they are equal, so the output is:

The first 5 characters of Hello World are equal to those of hello world

How $length changes the result

Only the first $length characters are inspected; anything after that index is invisible to the comparison. That makes strncasecmp() ideal for prefix matching.

<?php

// "image/png" vs "IMAGE/JPEG"
var_dump(strncasecmp("image/png", "IMAGE/JPEG", 6) === 0); // compares "image/" — true
var_dump(strncasecmp("image/png", "IMAGE/JPEG", 9) === 0); // now "png" vs "JPE" differs — false

Output:

bool(true)
bool(false)

With a length of 6 both strings share the prefix image/ (case-folded), so they're equal. Extend the window to 9 and the differing suffixes (png vs jpe) make them unequal.

Practical use: a case-insensitive prefix check

A common real-world need is "does this string start with X, regardless of case?" — for example, detecting a scheme in a URL.

<?php

function startsWithIgnoreCase(string $haystack, string $prefix): bool
{
    return strncasecmp($haystack, $prefix, strlen($prefix)) === 0;
}

var_dump(startsWithIgnoreCase("HTTPS://w3docs.com", "https://")); // true
var_dump(startsWithIgnoreCase("ftp://files.example", "https://")); // false

Output:

bool(true)
bool(false)

Passing strlen($prefix) as the length means you compare exactly the prefix and nothing more. On PHP 8 you could also use str_starts_with(), but that is case-sensitivestrncasecmp() is still the idiomatic choice when case must be ignored.

Gotchas

  • It returns an int, never a bool. Writing if (strncasecmp($a, $b, 3)) enters the block when the strings differ (non-zero is truthy) and skips it when they're equal (0 is falsy) — the opposite of what most people intend. Always compare explicitly: if (strncasecmp($a, $b, 3) === 0).
  • It is byte-based, not multibyte-aware. $length counts bytes, and case-folding only works for ASCII (A–Z). For accented or non-Latin text, use mb_strtolower() on both operands first, or mb_* comparison helpers.
  • A $length larger than either string is fine. The comparison simply stops at the end of the shorter string; you won't read out of bounds.
  • Empty strings compare equal at any length. strncasecmp("", "", 5) returns 0.
  • strncmp() — same bounded comparison, but case-sensitive.
  • strcasecmp() — case-insensitive comparison of the whole string.
  • strcmp() — case-sensitive comparison of the whole string.
  • substr_compare() — compare from an arbitrary offset, with an optional case-insensitive flag.
  • strpos() — find where one string occurs inside another.

Conclusion

strncasecmp() compares the first $length characters of two strings while ignoring case, returning a negative number, 0, or a positive number to signal the sort order. Reach for it when you need a case-insensitive prefix match or want to compare only the leading portion of two strings. Remember that the result is an integer — always test its sign explicitly — and that it works on bytes, so reach for the mb_* functions when handling multibyte text.

Practice

Practice
What is the purpose of the strncasecmp() function in PHP?
What is the purpose of the strncasecmp() function in PHP?
Was this page helpful?