Latest web development tutorials

PHP strcasecmp () function

PHP String Reference PHP String Reference

Examples

Compares two strings (case insensitive):

<?php
echo strcasecmp("Hello world!","HELLO WORLD!");
?>

Running instance »

Definition and Usage

strcasecmp () function compare two strings.

Tip: strcasecmp () function is binary safe and is not case sensitive.

Tip: This function strncasecmp () function is similar, the difference is that) you can specify the number of characters in each string for comparison by strncasecmp (.


grammar

strcasecmp( string1,string2 )

参数 描述
string1 必需。规定要比较的第一个字符串。
string2 必需。规定要比较的第二个字符串。

technical details

return value: This function returns:
  • 0 - if the two strings are equal
  • <0 - if string1 is less than string2
  • > 0 - if string1 is greater than string2
PHP version: 4+


More examples

Example 1

Compares two strings (case-insensitive, HELLO hELLo and outputs the same):

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

Running instance »

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
?>

Running instance »


PHP String Reference PHP String Reference