PHP strcasecmp()Functions
Examples
Compare two strings (case-insensitive):
<?php
echo strcasecmp("Hello world!","HELLO WORLD!");
?>
echo strcasecmp("Hello world!","HELLO WORLD!");
?>
Run example »
Definition and Usage
The strcasecmp() function compares two strings.
Tip:The strcasecmp() function is binary-safe and case-insensitive.
Tip:This function is similar to thestrncasecmp()strncasecmp() function, except that you can specify the number of characters to be used for comparison in each string.
Syntax
strcasecmp(string1,string2)
| Parameters | Description |
|---|---|
| string1 | Required. Specifies the first string to compare. |
| string2 | Required. Specifies the second string to compare. |
Technical Details
| Return value: | This function returns:
|
|---|---|
| PHP Version: | 4+ |
More Examples
Example 1
Compare two strings (case-insensitive, HELLO and hELLo output the same):
<?php
echo strcasecmp("Hello","HELLO");
echo "<br>";
echo strcasecmp("Hello","hELLo");
?>
echo strcasecmp("Hello","HELLO");
echo "<br>";
echo strcasecmp("Hello","hELLo");
?>
Run example »
Example 2
Different return values:
<?php
echo strcasecmp("Hello world!","HELLO WORLD!"); // The two strings are equal
echo strcasecmp("Hello world!","HELLO"); // String1 is greater than string2
echo strcasecmp("Hello world!","HELLO WORLD! HELLO!"); // String1 is less than string2
?>
echo strcasecmp("Hello world!","HELLO WORLD!"); // The two strings are equal
echo strcasecmp("Hello world!","HELLO"); // String1 is greater than string2
echo strcasecmp("Hello world!","HELLO WORLD! HELLO!"); // String1 is less than string2
?>
Run example »
PHP String Reference Other Extensions