PHP string comparison function Usage summary (STRCMP,STRCASECMP,STRNATCMP and strnatcasecmp) _php tips

Source: Internet
Author: User
Tags php programming php regular expression strcmp

This article analyses the usage of PHP string comparison function. Share to everyone for your reference, specific as follows:

You can use "= =" to compare strings directly, but sometimes you might want to do more complex string comparisons, such as partial matching.

1.STRCMP () function: This function is a comparison between strings, and is case-sensitive when compared.

Statement: strcmp (string str1,string str2)

The function compares the two string parameters passed in and returns 0 if the two strings are identical, returns a positive number if the str1 is followed by a dictionary order, and returns a negative number if str1 is less than str2.

Cases:

<?php
 $a = "I want to fly";
 $b = "I want to climb";
 $back = strcmp ($a, $b);
 if ($back >0)
  echo ' $a greater than $b ';
 ElseIf ($back <0)
  Echo ' $a less than $b ';
 else
  echo ' $a equals $b ';
? > 

Output results:

$a greater than $b

2.STRCASECMP (): This function is basically consistent with the strcmp function, but the function is case-insensitive when compared.

Cases:

<?php
  $val 1 = "Hello";
  $val 2 = "Hello";
  if (strcasecmp ($val 1, $val 2) ==0)
  Echo ' $val 1 and $val2 the same (ignoring the case of strings) ';
? >

Output results:

$val 1 is the same as $VAL2 (ignores the case of strings)

3. Natural sort strnatcmp (): This function is basically consistent with the usage of strcmp function, but the principle of comparison is different . This function is not arranged in dictionary order, but is sorted by "natural sort" Compare strings. The so-called natural sort is based on people's habits to sort, such as strcmp function to sort, "4" will be greater than "14", and in reality, the number "14" in Greater than "4", so the STRNATCMP function is based on the latter to compare.

4.STRNATCASECMP (): This function is consistent with the usage of the STRCASECMP function, except that the function is case-insensitive

For more information about PHP interested readers can view the site topics: "PHP string (String) Usage summary", "PHP Mathematical Arithmetic Skills summary", "PHP object-oriented Programming Introduction Course", "PHP Array" Operation Techniques Encyclopedia, " PHP Data structure and algorithm tutorial, "PHP Programming Algorithm Summary", "PHP Regular Expression Usage Summary", and "PHP Common database Operation Skills Summary"

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.