PHP strncmp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strncmp() compares the first N characters of two strings, and it is case-sensitive.

It is handy for prefix checks, such as testing whether a URL starts with "https://".

Syntax

Call strncmp() with the values you need. Start with this line.

PHP
$result = strncmp("apple pie", "apple tart", 5);
CallWhat it does
strncmp("apple pie", "apple tart", 5)Returns 0 because the first 5 characters match.
strncmp("Apple", "apple", 5)Returns a negative number because case is different.
strncmp($url, "https://", 8) === 0True when the URL starts with https://.

A negative length throws a ValueError in PHP 8. On PHP 8.0 and later, str_starts_with() is usually a cleaner choice for simple prefix checks.

Library declarations
PHP
strncmp(string $string1, string $string2, int $length): int

Parameters

ParameterDescription
string1Required. The first string.
string2Required. The second string.
lengthRequired. The number of characters to compare. Must not be negative.

Return Value

Return ValueDescription
intNegative if string1 is less than string2, 0 if the compared parts are equal, positive if string1 is greater.

PHP Examples

Example 1 — Compare the Start of Two Strings

Only the first five characters are checked.

PHP
<?php
$result = strncmp("apple pie", "apple tart", 5);

echo $result, PHP_EOL;

How It Works

  1. Pass two strings and a length of 5.
  2. PHP looks at the first five characters of each.
  3. Both start with "apple", so they match.
  4. The function returns 0.

Example 2 — Case-Sensitive Result

A different letter case counts as a difference.

PHP
<?php
$result = strncmp("Apple", "apple", 5);

echo $result === 0 ? "Match" : "No match", PHP_EOL;

How It Works

  1. Compare "Apple" and "apple" for 5 characters.
  2. The first letters differ in case.
  3. strncmp treats them as different.
  4. Use strncasecmp() if you want to ignore case.

Example 3 — Check a URL Prefix

Test whether a link is secure.

PHP
<?php
$url = "https://codetofun.com";

if (strncmp($url, "https://", 8) === 0) {
    echo "Secure link", PHP_EOL;
} else {
    echo "Not secure", PHP_EOL;
}

How It Works

  1. Store the URL in a variable.
  2. "https://" has 8 characters, so use 8 as the length.
  3. If the first 8 characters match, strncmp returns 0.
  4. Print a message based on the result.

Frequently Asked Questions

It compares the first N characters of two strings in a case-sensitive way and returns 0 if they are equal.
Yes. 'Apple' and 'apple' are different. Use strncasecmp() for case-insensitive comparison.
On PHP 8.0 and later, use str_starts_with($string, $prefix). On older versions, use strncmp($string, $prefix, strlen($prefix)) === 0.
PHP compares up to the end of the shorter string. A longer string is considered greater if the shorter one matches so far.
A negative number, 0, or a positive number. From PHP 8.2 the values are -1, 0, or 1.

Next: strpbrk()

strpbrk() searches a string for any one of several characters and returns the rest of the .

strpbrk() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful