PHP compares two strings of function strcasecmp ()

Source: Internet
Author: User

Instance

Compare two strings (case insensitive):

<?phpecho strcasecmp ("Hello world!", "Hello world!");? >

Definition and usage

The strcasecmp () function compares two strings.

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

Tip: This function is similar to the strncasecmp () function, but by strncasecmp () you can specify the number of characters each string is used to compare.

Grammar

STRCASECMP (STRING1,STRING2)
Parameters Describe
String1 Necessary. Specifies the first string to compare.
string2 Necessary. Specifies a second string to compare.

Technical details

return value: This function returns:
  • 0-if two strings are equal

  • <0-if string1 is less than string2

  • >0-if string1 is greater than string2

php version: 4+

More examples

Example 1

Compare two strings (case insensitive, hello and hello output are the same):

<?phpecho strcasecmp ("Hello", "Hello"), echo "<br>", Echo strcasecmp ("Hello", "Hello"); >

Example 2

Different return values:

<?phpecho strcasecmp ("Hello world!", "Hello world!"); The strings is Equalecho strcasecmp ("Hello world!", "Hello"); String1 is greater than String2echo strcasecmp ("Hello world!", "Hello world! Hello! "); String1 is less than string2?>

strcasecmp-binary Security comparison string (case insensitive)

int strcasecmp (String $str 1, String $str 2)

return value:

Returns a negative number if the str1 is less than str2, returns a positive number if the STR1 is greater than str2, and returns 0 if they are equal.

Simple example:

<?php$var1 = "Hello"; $var 2 = "Hello"; if (strcasecmp ($var 1, $var 2) = = 0) {    echo ' $var 1 is equal to $var 2 in a case-i Nsensitive string comparison ';}? >

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.