How to use php strcasecmp function?

藏色散人
Release: 2023-02-22 22:26:02
Original
2443 people have browsed it

php The strcasecmp function is used to compare two strings. Its syntax is strcasecmp(string1,string2). The parameter string1 is required and refers to the first string to be compared; string2 is required and refers to the second string to be compared. String.

How to use php strcasecmp function?

#php How to use strcasecmp function?

Definition and usage

strcasecmp() function compares two strings.

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

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

Parameters

string1 Required. Specifies the first string to compare.

string2 Required. Specifies the second string to be compared.

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

Example 1

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

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

Output:

0
0
Copy after login

Example 2

Different return values:

<?php
echo strcasecmp("Hello world!","HELLO WORLD!"); // 两字符串相等
echo strcasecmp("Hello world!","HELLO"); // string1 大于 string2
echo strcasecmp("Hello world!","HELLO WORLD! HELLO!"); // string1 小于 string2
?>
Copy after login

Output:

0
7
-7
Copy after login

Example 3

Compare two strings (case-insensitive):

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

Output:

0
Copy after login

The above is the detailed content of How to use php strcasecmp function?. 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