PHP strncasecmp() Function

Example

Compare two strings (case-insensitive):

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

Run Instances

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 different from strcasecmp(), it does not have length Parameters.

Syntax

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

Technical Details

Return value:

This function returns:

  • 0 - If the 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 Instances