Home >>PHP String Functions >PHP strncmp() Function
PHP strncmp() function is used to compare two given input strings. It is case-sensitive and binary-safe. It is similar to strcmp() function but the only difference is that strcmp() function doesn’t have the length parameter.
Syntax:
strncmp($string1, $string2, $length);
Parameter | Description |
---|---|
string1 | This is a required parameter. This parameter defines the first string to compare. |
string2 | This is a required parameter. This parameter defines the second string to compare. |
length | This is a required parameter. This parameter defines the number of characters from each string to be used in the comparison. |
Here is an example of strncmp() function in PHP:
<html> <body> <?php echo strncmp("PHPTPOINT","PHPTPOINT..",8); // returns zero when both the strings are same. ?> </body> </html>
Here is an another example of strncmp() function in PHP:
<html> <body> <?php $str = "PHPTPOINT"; echo strncmp($str,"PHP",2); // case sensitive echo "<br>"; echo strncmp($str,"PHPT",3); echo "<br>"; echo strncmp($str,"PHPT",7); // first string is greater than second string. ?> </body> </html>