Comparison of strings in php

Source: Internet
Author: User
This article introduces one or two character comparison methods in php: the number and the strcmp function for equal character string comparison. For more information, see. Use the strcmp () function string to compare intstrcmp (stringstr1, stringstr2 )).

This article introduces one or two character comparison methods in php: =, and strcmp for equal character string comparison. For more information, see.

Use strcmp () function strings for comparison

Int strcmp (string str1, string str2 ))

The str1 and str2 parameters define the two strings to be compared. if they are equal, 0 is returned. if str1 is greater than str2, the return value is greater than 0. if str1 is smaller than str2, the return value is smaller than 0.

The instance code is as follows:

  1. $ Str1 = "a"; // defines a string constant
  2. $ Str2 = "B"; // defines a string constant.
  3. $ Str3 = "ccc"; // defines a string constant.
  4. $ Str4 = "CCC"; // defines a string constant.
  5. Echo strcmp ($ str1, $ str2); // The two strings are equal.
  6. Echo strcmp ($ str3, $ str4); // note that this function is case sensitive.
  7. ?>

Note: This function is case sensitive.

Use = for comparison

The code is as follows:

  1. $ A = "joe ";
  2. $ B = "jerry ";
  3. If ($! = $ B)
  4. {
  5. Echo "not equal ";
  6. }
  7. Else
  8. {
  9. Echo "equal ";
  10. }

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.