strncmp() – PHP String Functions

Syntax :

int strncmp ( string1, string2, length );

Description :

strncmp() function will compare two strings. This is case-sensitive function.

Note : Binary safe and case-sensitive function.

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

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description

string1, Required, String, It is the first string which will be compared.

string2, Required, String, It is the second string which will be compared.

length, Required, Integer, It specifies the number of characters from each string to be used in the comparison.

[/table]


Output :

Return values in this function are:

  • 0 – if the two strings are equal.
  • < 0 – if string1 is less than string2.
  • > 0 – if string1 is greater than string2.

Related articles : strncasecmp()strncasecmp(), strcmp()substr_compare()strcasecmp()strstr()substr() .


strncmp() – PHP Functions Example 1 : It returns 0, if the two strings are equal.
<?php
// case-sensitive comparison
echo strncmp("Hi from tutorialmines.","Hi from tutorialmines.",8);
?>

Output of above code in the browser is as below:

0

strncmp() – PHP Functions Example 2 : It returns 0, if the two strings are equal. Case of character matter in this function. “Hi from tutorialmines.” is different from “hI from Tutorialmines.”.
<?php
echo strncmp("Hi from tutorialmines.","Hi from tutorialmines.",8);
echo "<br/>";
echo strncmp("Hi from tutorialmines.","hI from Tutorialmines.",8);
?>

Output of above code in the browser is as below:

0
-32

 

You may also like...