strncmp
Binary safe comparison of several characters at the beginning of a string
strncmp()
function compares two strings.
Note: strncmp()
is binary-safe and case-sensitive.
Tip: This function is similar to the strcmp()
function. The difference is that strcmp()
does not have a length parameter.
Compare two strings (case sensitive):
<?php echo strncmp ( "I love China!" , "I love Shanghai!" , 6 ) ; ?>
Try it yourself
Comparing two strings (case sensitive, China and CHINA outputs are different):
<?php echo strncmp ( "China" , "China" , 6 ) ; echo "<br>" ; echo strncmp ( "China" , "CHINA" , 6 ) ; ?>
Try it yourself
strncmp ( string1 , string2 , length )
parameter | describe |
---|---|
string1 | Required. Specifies the first string to be compared. |
string2 | Required. Specifies the second string to be compared. |
length | Required. Specifies the number of characters for each string used in the comparison. |