Home >Backend Development >PHP Tutorial >String comparison using PHP string functions

String comparison using PHP string functions

WBOY
WBOYOriginal
2023-06-20 09:06:441569browse

In PHP, string comparison is a very common operation. String comparison can be used to determine whether two strings are equal and compare the sizes of strings to perform corresponding logical processing. In PHP, many string comparison functions are provided, which can be used according to different needs. This article will introduce some common PHP string comparison functions and their applications.

  1. strcmp

The strcmp function is used to compare the sizes of two strings. It returns an integer value indicating the size relationship between the two strings. If equal, return 0; if the first parameter string is greater than the second parameter string, return an integer greater than 0; if the first parameter string is less than the second parameter string, return an integer less than 0. The syntax format is:

int strcmp ( string $str1 , string $str2 )

For example:

$str1 = "Hello world!";
$str2 = "Hello world!";
$str3 = "Hello php!";

echo strcmp($str1, $str2); // 0
echo strcmp($str1, $str3); // -21

In the above example, $str1 and $str2 are equal, the return value is 0; $str1 is greater than $str3, the return value is -21 .

  1. strcasecmp

The strcasecmp function compares two strings without case sensitivity. Its usage and return value are the same as the strcmp function. The syntax format is:

int strcasecmp ( string $str1 , string $str2 )

For example:

$str1 = "Hello World!";
$str2 = "hello world!";

echo strcasecmp($str1, $str2); // 0

In the above example, $str1 and $str2 are equal, and the return value is 0.

  1. strncmp

The strncmp function is used to compare the first $n characters of two strings, and its return value is the same as the strcmp function. The syntax format is:

int strncmp ( string $str1 , string $str2 , int $n )

For example:

$str1 = "Hello world!";
$str2 = "Hello php!";

echo strncmp($str1, $str2, 5); // 0

In the above example, the first 5 characters of $str1 and $str2 are compared, and the return value is 0.

  1. strncasecmp

strncasecmp function is used to compare the first $n characters of two strings. It is not case-sensitive and returns an integer value. Its usage method is as follows The return value is the same as the strcmp function. The syntax format is:

int strncasecmp ( string $str1 , string $str2 , int $n )

For example:

$str1 = "Hello World!";
$str2 = "hello php!";

echo strncasecmp($str1, $str2, 5); // 0

In the above example, the first 5 characters of $str1 and $str2 are compared, case-insensitive, and the return value is 0.

  1. strcoll

The strcoll function is used to compare the localized sorting of two strings, and its return value is the same as the strcmp function. The syntax format is:

int strcoll ( string $str1 , string $str2 )

For example:

$str1 = "会";
$str2 = "火车";

echo strcoll($str1, $str2); // -1

In the above example, because the order and letters of Chinese characters are different, -1 is returned.

In actual development, string comparison is a very common operation. Understanding the use and characteristics of string comparison functions can help us perform better string operations.

The above is the detailed content of String comparison using PHP string functions. 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