Example of the difference between PHP strcmp () and strcasecmp () _php instance

Source: Internet
Author: User
Tags first string strcmp

This article mainly introduced PHP strcmp () and strcasecmp () function string comparison usage, combined with the case form in detail analysis of the strcmp () and strcasecmp () function, the use of methods and differences

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.

The ps:strcmp () 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.

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

Sample code:

<?php
 $str 1 = "Code Agriculture Tutorial";
 $str 2 = "Code agriculture Tutorial";
 $str 3= "www.jmanongjc.com";
 $str 4= "Www.jmanongjc.COM";
 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
?>


Results:

"; Echo strcmp ($str 3, $str 4);//Note that the function is case-sensitive echo "
"; Echo strcasecmp ($str 3, $str 4);//The function is case-insensitive?>

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

Thank you for reading, I hope to help you, thank you for your support for this site!

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.