PHP strcmp() Function
Example
Compare two strings (case-sensitive):
<?php echo strcmp("Hello world!","Hello world!"); ?>
Definition and Usage
The strcmp() function compares two strings.
Note:The strcmp() function is binary safe and case-sensitive.
Tip:This function is similar to strncmp() Functions similar, but unlike strcmp(), you can specify the number of characters used for comparison in strncmp().
Syntax
strcmp(string1,string2)
Parameters | Description |
---|---|
string1 | Required. Specifies the first string to be compared. |
string2 | Required. Specifies the second string to be compared. |
Technical Details
Return value: |
This function returns:
|
PHP Version: | 4+ |
More Examples
Example 1
Compare two strings (case-sensitive, Hello and hELLo output is different):
<?php echo strcmp("Hello","Hello"); echo "<br>"; echo strcmp("Hello","hELLo"); ?>
Example 2
Different return values:
<?php echo strcmp("Hello world!","Hello world!"); // Two strings are equal echo strcmp("Hello world!","Hello"); // string1 Greater than string2 echo strcmp("Hello world!","Hello world! Hello!"); // string1 Less than string2 ?>