PHP string comparison

Source: Internet
Author: User
Tags comparison first string strcmp

1. String comparisons by Byte:

The strcmp () and strcasecmp () functions are used to compare two strings by byte.

Syntax format:

int strcmp (string str1,string str2)

int strcasecmp (string str1,string str2)

If STR1=STR2, the return value is 0; if STR1>STR2, the return value >0; if STR1<STR2, the return value is less than 0

The difference between the two functions is that strcmp () distinguishes the case of the characters, while STRCASECMP () does not distinguish between the case of characters

2, according to natural sorting method for string comparison: strmatcmp () function

Grammar:

int strnatcmp (string str1,string str2)

Returns 0 if the string is equal, and if STR1>STR2, the return value is greater than 0; if STR1<STR2, the return value is less than 0

3. Specify a comparison from the position of the source string: strncmp () function

Grammar:

int strncmp (string str1,string str2,int len)

Description

STR1: Specifies the first string to participate in the comparison

STR2: Specifies the second string to participate in the comparison

Len: Specifies the number of characters involved in the comparison in each string

If the string is equal, return 0 if STR1>STR2, return value >0; if STR1<STR2, return value <0

Example: Compare the first three characters of two strings for equality

<?php
$a = "www.bianceng.cn";
$b = "www.bianceng.com";
Echo strncmp ($a, $b, 3);
?>

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.