Facebook Page Twitter Page LinkedIn Page
× PHP Tutorial PHP Advanced PHP Resources


The PHP String strncasecmp() function is used to compare two strings. Based on the value of two strings, the function returns the following:

  • Returns a number less than 0, when first string is less than second string.
  • Returns 0, when both strings are equal.
  • Returns a number greater than 0, when first string is greater than second string.

This function is same as strcmp() function, except strncasecmp() function has one more parameter which is used to specify number of characters of each string to compare.

Note: The strcasecmp() function is a binary-safe but not a case-sensitive function.

Syntax

strncasecmp(str1, str2, length)

Parameters

str1 Required. Specify the first string to compare.
str2 Required. Specify the second string to compare.
length Required. Specify number of characters of each string to compare.

Return Value

Returns a number less than 0, if first string is less than second string.
Returns 0, if both strings are equal.
Returns a number greater than 0, if first string is greater than second string.

Example:

In the below example, strncasecmp() function is used to compare two strings and returns values based on the value of two strings.

<?php
echo strncasecmp('HELLO WORLD', 'hello C#', 5)."\n";
echo strncasecmp('World', 'Hello', 5)."\n";
echo strncasecmp('Hello', 'World', 5)."\n";
?>

The output of the above code will be:

0
15
-15

❮ PHP String functions