String comparison usage analysis of strcmp() and strcasecmp() functions in PHP_php tips

WBOY
Release: 2016-05-16 20:00:39
Original
1150 people have browsed it

The example in this article describes the usage of strcmp() and strcasecmp() function string comparison in PHP. Share it with everyone for your reference, the details are as follows:

1. The strcmp() function in PHP is used to compare two strings (case sensitive) , and its definition is as follows:

strcmp(string1,string2)

Parameter description:

string1 Required. Specifies the first string to compare.
string2 required. Specifies the second string to be compared.

PS: The strcmp() function is similar to the strncmp() function. The difference is that strncmp() can specify the number of characters for each string to be compared.

2. The strcasecmp() function in PHP compares two strings (case-insensitive) , which is defined as follows:

strcasecmp(string1,string2)

Parameter description:

string1 required. Specifies the first string to compare.
string2 required. Specifies the second string to be compared.

PS: The strcasecmp() function is similar to the strncasecmp() function. The difference is that strncasecmp() can specify the number of characters for each string to be compared.

Sample code:

<&#63;php
 $str1="脚本之家";
 $str2="脚本之家";
 $str3="www.jb51.net";
 $str4="WWW.JB51.NET";
 echo strcmp($str1,$str2);//两个字符串相等
 echo "<br/>";
 echo strcmp($str3,$str4);//注意该函数区分大小写
 echo "<br/>";
 echo strcasecmp($str3,$str4);//该函数不区分大小写
&#63;>

Copy after login

The running results are as follows:

0
1
0

Copy after login

Supplement: Regarding the return value issue

Comparison of parameters string1 and string2:

If the two strings are equal, the return value is 0
If string1 is less than string2, the return value is less than 0
If string1 is greater than string2, the return value is greater than 0

I hope this article will be helpful to everyone in PHP programming.

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