Current Location: Home> Function Categories> strncmp

strncmp

Binary safe comparison of several characters at the beginning of a string
Name:strncmp
Category:String
Programming Language:php
One-line Description:Comparison of strings for the first n characters (case sensitive).

Definition and usage

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.

Example

Example 1

Compare two strings (case sensitive):

 <?php
echo strncmp ( "I love China!" , "I love Shanghai!" , 6 ) ;
?>

Try it yourself

Example 2

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

grammar

 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.
Similar Functions
Popular Articles