PHP strncasecmp() function

Instance

Compare two strings (case insensitive):

<?php
echo strncasecmp("I love China!","I love Shanghai!",6);
?>

Run Example

Definition and Usage

The strncasecmp() function compares two strings.

Note:strncasecmp() is binary safe and case insensitive.

Tip:This function is similar to strcasecmp() The function is similar, but unlike strcasecmp(), it does not have length Parameters.

Syntax

strncasecmp(string1,
string2,length)
Parameters Description
string1 Required. Specifies the first string to be compared.
string2 Required. Specifies the second string to be compared.
length Required. Specifies the number of characters used to compare each string.

Technical Details

Return value:

This function returns:

  • 0 - If two strings are equal
  • <0 - If string1 Less than string2
  • >0 - If string1 Greater than string2
PHP Version: 4+

More Examples

Example 1

Compare two strings (case insensitive, China and CHINA output the same):

<?php
echo strncasecmp("China","China",6);
echo "<br>";
echo strncasecmp("China","CHINA",6);
?>

Run Example