PHP strncmp() Function

Example

Compare Two Strings (Case Sensitive):

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

Run Instance

Definition and Usage

The strncmp() function compares two strings.

Note:strncmp() is binary safe and case sensitive.

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

Syntax

strncmp(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 in each string for the comparison.

Technical Details

Return Value:

This function returns:

  • 0 - If the two strings are equal
  • <0 - If string1 is less than string2
  • >0 - If string1 is greater than string2
PHP Version: 4+

More Examples

Example 1

Compare Two Strings (Case Sensitive, Output is different for 'China' and 'CHINA'):

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

Run Instance