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

PHP function strcasecmp() that compares two strings

黄舟
Release: 2023-03-17 06:14:02
Original
1550 people have browsed it

Example

Compare two strings (case-insensitive):

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

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)
Copy after login
ParametersDescription
string1 Required. Specifies the first string to compare.
string2Required. 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

  • ##<0 - if string1 is less than string2

  • >0 - 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");
?>
Copy after login

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 
?>
Copy after login

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

int strcasecmp ( string $str1 , string $str2 )
Copy after login

Return value:

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

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;;
}
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template