Current Location: Home> Function Categories> strcmp

strcmp

Binary safe string comparison
Name:strcmp
Category:String
Programming Language:php
One-line Description:Compare two strings (case sensitive).

Definition and usage

strcmp() function compares two strings.

Note: The strcmp() function is binary-safe and case-sensitive.

Tip: This function is similar to the strncmp() function, except that through strncmp() you can specify the number of characters each string is used for comparison.

Example

Example 1

Compare two strings (case sensitive):

 <?php
echo strcmp ( "Hello world!" , "Hello world!" ) ;
?>

Try it yourself

Example 2

Comparing two strings (case sensitive, Hello and hELLo outputs are different):

 <?php
echo strcmp ( "Hello" , "Hello" ) ;
echo "<br>" ;
echo strcmp ( "Hello" , "hELLo" ) ;
?>

Try it yourself

Example 3

Different return values:

 <?php
echo strcmp ( "Hello world!" , "Hello world!" ) ; // Two strings are equal
echo strcmp ( "Hello world!" , "Hello" ) ; // string1 is greater than string2
echo strcmp ( "Hello world!" , "Hello world! Hello!" ) ; // string1 is less than string2
?>

Try it yourself

grammar

 strcmp ( string1 , string2 )
parameter describe
string1 Required. Specifies the first string to be compared.
string2 Required. Specifies the second string to be compared.
Similar Functions