PHP strncmp()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Compare two strings (case-sensitive):

<?php
echo strncmp("Hello world!","Hello earth!",6);
?>

Run example »

Definition and Usage

The strncmp() function compares two strings (case-sensitive).

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

Tip:This function is similar tostrcmp()the function, except that strcmp() does not have the length parameter.


Syntax

strncmp(string1,string2,length)

Parameter Description
string1 Required. Specifies the first string to compare.
string2 Required. Specifies the second string to compare.
length Required. Specifies the number of characters from each string to compare.

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, Hello and hELLo output are different):

<?php
echo strncmp("Hello","Hello",6);
echo "<br>";
echo strncmp("Hello","hELLo",6);
?>

Run example »


PHP String 参考手册PHP String Reference Manual Other Extensions