The strncmp() function compares two strings case-sensitive for specified length.
PHP strncmp() Function has the following syntax.
strncmp(string1,string2,length)
Parameter | Is Required | Description |
---|---|---|
string1 | Required. | First string to compare |
string2 | Required. | Second string to compare |
length | Required. | Number of characters from each string to be used in the comparison |
This function returns:
Compare two strings (case-sensitive):
<?php
echo strncmp("Hello world!","Hello earth!",6);
?>
The code above generates the following result.
Compare two strings (case-sensitive = Hello and hELLo will not output the same):
<?php
echo strncmp("Hello","Hello",6);
echo "<br>";
echo strncmp("Hello","hELLo",6);
?>
The code above generates the following result.