Latest web development tutorials

PHP strncmp () function

PHP String Reference PHP String Reference

Examples

Compares two strings (case sensitive):

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

Running instance »

Definition and Usage

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

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

Tip: This function strcmp () function is similar, except that, strcmp () no length parameter.


grammar

strncmp( string1,string2,length )

参数 描述
string1 必需。规定要比较的第一个字符串。
string2 必需。规定要比较的第二个字符串。
length 必需。规定每个字符串用于比较的字符数。

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

Compares two strings (case sensitive, Hello and hELLo output are not the same):

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

Running instance »


PHP String Reference PHP String Reference