PHP strcasecmp() Function

Example

Compare two strings (case insensitive):

<?php
echo strcasecmp("shanghai","SHANGHAI");
?>

Run Instance

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 strncasecmp() Functions are similar, but differently, through strncasecmp() you can specify the number of characters used for comparison in each string.

Syntax

strcasecmp(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:

The function returns:

  • 0 - If two strings are equal
  • <0 - If string1 Less than string2
  • >0 - If string1 Greater than string2
PHP Version: 4+

More Examples

Example 1

Compare two strings (case-insensitive, HELLO and hELLo output the same):

<?php
echo strcasecmp("Shanghai","SHANGHAI");
echo "<br>";
echo strcasecmp("Shanghai","sHANGHai");
?>

Run Instance

Example 2

Different Return Values:

<?php
echo strcasecmp("Hello world!","HELLO WORLD!"); // Two strings are equal
echo strcasecmp("Hello world!","HELLO"); // string1 Greater than string2
echo strcasecmp("Hello world!","HELLO WORLD! HELLO!"); // string1 Less than string2
?>

Run Instance