Home > Backend Development > PHP Tutorial > Analysis on string comparison usage of strcmp() and strcasecmp() functions in PHP, strcmpstrcasecmp_PHP tutorial

Analysis on string comparison usage of strcmp() and strcasecmp() functions in PHP, strcmpstrcasecmp_PHP tutorial

WBOY
Release: 2016-07-12 09:01:16
Original
1188 people have browsed it

Analysis on the comparison usage of strcmp() and strcasecmp() function strings in PHP, strcmpstrcasecmp

The examples in this article describe the comparison usage of strcmp() and strcasecmp() function strings 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.

Articles you may be interested in:

  • How to obtain the number of occurrences of a substring with the substr_count() function in PHP
  • After PHP uses the strstr() function to obtain the specified string Methods for all characters
  • The strncmp() function in PHP compares whether the first 2 characters of two strings are equal
  • The strnatcmp() function "natural sorting algorithm" in PHP performs string comparison Usage analysis (compared to the strcmp function)
  • Usage analysis of substr function string interception in PHP
  • PHP uses the trim function to remove left and right spaces and special characters from a string Example
  • PHP encapsulated String encryption and decryption function
  • The most accurate PHP function to intercept string length
  • What to do if the problem of Chinese garbled characters occurs when substr() is used to intercept strings in PHP
  • Commonly used in PHP Summary of String Operation Function Examples

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1089579.htmlTechArticleStrcmp() and strcasecmp() function string comparison and usage analysis in PHP, strcmpstrcasecmp This article describes strcmp in PHP with examples () and strcasecmp() function string comparison usage. Share it with everyone...
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