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.
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 .
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.
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.
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.
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!