strncmp()
The strncmp() function in PHP is used to compare two strings up to a certain length. It is used to compare the first N characters of two strings, where N is the
Introduction
The strncmp() function in PHP performs a binary-safe, case-sensitive comparison of the first N characters of two strings. Instead of comparing the strings in full, it only looks at the leading portion you specify — which makes it the right tool whenever you care about a prefix rather than the whole value (for example, checking whether a URL starts with https, or grouping codes that share a common header).
This article covers the syntax of strncmp(), how to read its return value, several practical examples, and the common gotchas that trip people up.
Syntax
strncmp(string $string1, string $string2, int $length): int| Parameter | Description |
|---|---|
$string1 | The first string to compare. |
$string2 | The second string to compare. |
$length | The maximum number of characters to compare from the start of each string. |
strncmp() compares the first $length characters of $string1 and $string2 byte by byte and returns an integer:
| Return value | Meaning |
|---|---|
< 0 (negative) | $string1 is "less than" $string2 within the compared range. |
0 | The first $length characters are equal. |
> 0 (positive) | $string1 is "greater than" $string2 within the compared range. |
The comparison is lexicographic, based on the byte value of each character. Because uppercase ASCII letters (A–Z, codes 65–90) come before lowercase ones (a–z, codes 97–122), "Apple" is considered less than "apple". The exact non-zero number is the byte-value difference of the first differing character, but you should rely only on its sign, not its magnitude.
Example: comparing a prefix
<?php
$string1 = "Hello World";
$string2 = "Hello";
$length = 5;
$result = strncmp($string1, $string2, $length);
if ($result < 0) {
echo "The first $length characters of $string1 are less than the first $length characters of $string2";
} elseif ($result > 0) {
echo "The first $length characters of $string1 are greater than the first $length characters of $string2";
} else {
echo "The first $length characters of $string1 are equal to the first $length characters of $string2";
}Even though "Hello World" and "Hello" differ overall, their first 5 characters ("Hello") are identical, so strncmp() returns 0 and the output is:
The first 5 characters of Hello World are equal to the first 5 characters of HelloExample: checking whether a string starts with a prefix
A classic use of strncmp() is testing for a prefix. By passing the prefix's length as $length, you compare only as much of the subject as the prefix covers:
<?php
$url = "https://www.w3docs.com";
if (strncmp($url, "https", 5) === 0) {
echo "Secure URL";
} else {
echo "Not secure";
}This prints Secure URL. On PHP 8.0+ the dedicated str_starts_with() function expresses the same intent more clearly, but strncmp() remains the portable choice for older versions.
Example: it is case-sensitive
strncmp() distinguishes uppercase from lowercase:
<?php
echo strncmp("PHP", "php", 3); // negative: 'P' (80) < 'p' (112)If you need to ignore case, use strncasecmp() instead, which performs the same length-limited comparison without case sensitivity.
Common gotchas
- Only the sign is meaningful. Treat the result as "negative / zero / positive", and prefer
=== 0when you mean "the prefixes match". Don't assume the value is exactly-1,0, or1— it can be any integer. $lengthlarger than the strings is fine. If$lengthexceeds the length of either string,strncmp()simply compares up to the end of the shorter one.strncmp("Hi", "Hi", 50)returns0.- A negative
$lengththrows aValueErroron PHP 8.0+ (and was treated as0on older versions). - It is byte-based, not multibyte-aware. For UTF-8 text,
$lengthcounts bytes, not characters, so a multi-byte character may be split. Plain ASCII data is unaffected.
Related functions
strcmp()— compares two full strings, case-sensitively.strncasecmp()— likestrncmp(), but case-insensitive.strcasecmp()— case-insensitive comparison of full strings.substr_compare()— compares strings starting at a chosen offset.strpos()— finds the position of a substring.
Conclusion
strncmp() compares only the first N characters of two strings, case-sensitively and binary-safely, returning a negative number, 0, or a positive number. Reach for it when you need to match a prefix or compare a fixed-length leading portion of two strings — and remember to check the sign of the result with === 0, < 0, or > 0 rather than an exact value.