Home  >  Article  >  Backend Development  >  PHP function strcasecmp() that compares two strings

PHP function strcasecmp() that compares two strings

黄舟
黄舟Original
2017-11-03 14:15:281498browse

Example

Compare two strings (case-insensitive):

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

Definition and usage

strcasecmp() Function Compares two strings.

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

Tip: This function is similar to the strncasecmp() function, except that with strncasecmp() you can specify the number of characters for each string to be compared.

Syntax

strcasecmp(string1,string2)
Parameters Description
string1 Required. Specifies the first string to compare.
string2 Required. Specifies the second string to be compared.

Technical details

##PHP version: More Multiple instances
Return value: This function returns:
  • 0 - if the two strings are equal

  • ##8f029751ac12f19769820fc6523b37120 - if string1 Greater than string2

4+

Instance 1

Compare two strings (not case sensitive, HELLO and hELLo output the same):

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

Instance 2

Different Return value:

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

strcasecmp — Binary safe comparison of strings (case-insensitive)

int strcasecmp ( string $str1 , string $str2 )

Return value:

//如果 str1 小于 str2,返回负数;如果 str1 大于 str2,返回正数;二者相等则返回 0。

Simple example:

<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
    echo &#39;$var1 is equal to $var2 in a case-insensitive string comparison&#39;;
}
?>

The above is the detailed content of PHP function strcasecmp() that compares two strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn