PHP strcmp () and strcasecmp () function string comparison usage analysis _php techniques

Source: Internet
Author: User
Tags first string strcmp

The examples in this article describe the comparison usage of strcmp () and strcasecmp () function strings in PHP. Share to everyone for your reference, specific as follows:

The strcmp () function in PHP is used to compare two strings (case sensitive), which is defined as follows:

strcmp (STRING1,STRING2)

Parameter description:

String1 required. Specify the first string to compare.
string2 required. Specify the second string to compare.

PS: Thestrcmp () function is similar to the strncmp () function, and strncmp () can specify the number of characters per string to compare.

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

STRCASECMP (STRING1,STRING2)

Parameter description:

String1 required. Specify the first string to compare.
string2 required. Specify the second string to compare.

PS: Thestrcasecmp () function is similar to the strncasecmp () function, and strncasecmp () can specify the number of characters per string to compare.

Sample code:

<?php
 $str 1 = "cloud-dwelling community";
 $str 2 = "cloud-dwelling community";
 $str 3= "Www.jb51.net";
 $str 4= "WWW.JB51.NET";
 Echo strcmp ($str 1, $str 2);//Two strings equal
 echo "<br/>";
 Echo strcmp ($str 3, $str 4);//Note that the function is case-sensitive and case
 echo "<br/>";
 Echo strcasecmp ($str 3, $str 4);//This function is case-insensitive
?>

The results of the operation are as follows:

0
1
0

Add: About return value issues

For the comparison of parameter string1 and string2:

If 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 help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.