Php character comparison strcmp () function usage

Source: Internet
Author: User
The strcmp () function is used to compare two strings. if the two strings are completely equal, 0 is returned. otherwise, various return values are returned. This function is case-sensitive and can be used as a reference. The return value after comparison indicates that if str1 and str2 are equal, 0 is returned.

The strcmp () function is used to compare two strings. if the two strings are completely equal, 0 is returned. otherwise, various return values are returned. This function is case-sensitive and can be used as a reference.

Return value after Comparison

If str1 and str2 are equal, 0 is returned.

If str1 is smaller than str2, return <0 (but not necessarily scheduled-1, many online say-1, it is incorrect, the specific value size depends on the actual situation ).

If str1 is greater than str2, return> 0 (but different timing 1, the specific value size depends on the actual situation ).

The instance code is as follows:

  1. Strcmp (date ('Y-m-D', $ user-> last_time), date ("Y-m-d", time () = 0
  2. If case insensitive

Strncasecmp is used to compare a part of a case-insensitive string. it starts from the beginning of the string and the third parameter is the length to be compared:

The instance code is as follows:

  1. Echo strncasecmp ("abcdd", "abcde", 3 );
  2. // Return 0, which compares abc and abc. The two are the same because they are case insensitive.

We can also use = to compare php strings, as shown in

The instance code is as follows:

  1. $ A = 'a ';
  2. $ B = 'www .phpfensi.com ';
  3. If ($ a = $ B)
  4. {
  5. Echo 'Equality ';
  6. }
  7. Else
  8. {
  9. Echo 'unequal ';
  10. }

You can also use = to compare

The instance code is as follows:

  1. 22 = "22"; // return true
  2. 22 = "22"; // Return false

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.