PHP strncasecmp() Functions

Example

Compare two strings (case-insensitive):

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

Run Examples

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 differs from strcasecmp() in that it does not have length Parameters.

Syntax

strncasecmp(string1,
string2,length)
Parameters Description
string1 Required. Specify the first string to be compared.
string2 Required. Specify the second string to be compared.
length Required. Specify the number of characters used for comparison 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 Examples