PHP strcasecmp() Function

Mga halimbawa

Pagsamahin ang dalawang string (hindi nasisiwalat ang laki ng mga titik):

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

Run Example

Definition and Usage

The strcasecmp() function compares two strings.

Tip:The strcasecmp() function is binary safe and case-insensitive.

Tip:The function is similar to strncasecmp() The function is similar, but differently, by using strncasecmp() you can specify the number of characters of each string used for comparison.

Syntax

strcasecmp(string1,string2)
Parameter 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 Example

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 Example