Home>Article>Backend Development> How to determine whether two strings are equal in php

How to determine whether two strings are equal in php

青灯夜游
青灯夜游 Original
2022-08-17 19:40:36 7311browse

5 methods: 1. Use "String 1==String 2", if true is returned, they are equal. 2. Use "strcmp(String 1, String 2)", if the return value is 0, it is equal. 3. Use "strcasecmp(String 1, String 2)", if the return value is 0, it is equal. 4. Use "strncasecmp(string 1, string 2, number of characters)". If the return value is 0, it is equal. 5. Use "strncmp(String 1, String 2, Number of characters)". If the return value is 0, it is equal.

How to determine whether two strings are equal in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

Determine whether two strings are equal? There are multiple functions built into PHP to achieve this:

  • strcmp()

  • strcasecmp()

  • strncasecmp()

  • strncmp()

You can also use the equality operator "==" for comparison. Let’s take a look below:

1. Use the operator “==” to compare whether strings are equal.

“==” means “equal”, Is a comparison operator used to compare whether expressions or variables on both sides of the equal sign are equal.

How to determine whether two strings are equal in php

2. Use the strcmp() function to compare whether strings are equal

strcmp(): Compare two strings ( Case Sensitive).

strcmp(字符串1,字符串2)

The return value is divided into 3 situations:

  • 0< code> - If the two strings are equal 0<>

  • - if String1is less thanString2

  • >0- if String1is greater thanString2

You only need to check whether the return value of the strcmp() function is equal to 0 to know whether the two strings are equal.

How to determine whether two strings are equal in php

Note that for the strcmp() function, the two strings to be compared must match exactly to be considered equal. For example, $pwd1 and $pwd2 in the example code are different. If you want to compare two strings in a case-insensitive manner, you can use the strcasecmp() function.

3. Use the strcasecmp() function to compare whether strings are equal.

The strcasecmp() function in PHP has similar functions to the strcmp() function. Both can compare two strings. string, the difference is that the strcasecmp() function is not case-sensitive when comparing strings. Its syntax format is as follows:

strcasecmp($str1,$str2)

According to the comparison result,

  • If $str1 is less than $str2, the return value is

  • If $str1 is greater than $str2, the return value is > 0;

  • If $str1 is equal to $str2, return 0.

You only need to check whether the return value of the strcasecmp() function is equal to 0 to know whether the two strings are equal.

How to determine whether two strings are equal in php

4. Use the strncasecmp() function to compare whether strings are equal

strncasecmp() function compares two strings ( not case sensitive).

This function is similar to the strcasecmp() function, except that strcasecmp() has no length parameter (the number of characters for each string to be compared cannot be specified).

strncasecmp(string1,string2,length)
Parameters Description
string1 Required . Specifies the first string to compare.
string2 Required. Specifies the second string to be compared.
length Required. Specifies the number of characters per string used for comparison.

The return value is the same as strcasecmp() function

How to determine whether two strings are equal in php

5. Use strncmp() function to compare Whether the strings are equal

strncmp() function compares two strings (case sensitive).

This function is similar to the strcmp() function, except that strcmp() does not have a length parameter (the number of characters for each string to be compared cannot be specified).

strncmp(string1,string2,length)
Parameters Description
string1 Required . Specifies the first string to compare.
string2 Required. Specifies the second string to be compared.
length Required. Specifies the number of characters per string used for comparison.

返回值和strcmp() 函数一样

How to determine whether two strings are equal in php

推荐学习:《PHP视频教程

The above is the detailed content of How to determine whether two strings are equal in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn