PHP 字符串 strcoll()函數(shù)
strcoll() 是PHP的內置字符串函數(shù),用于比較兩個字符串。愛掏網(wǎng) - it200.com它是基于本地化的字符串比較。愛掏網(wǎng) - it200.com
需要注意的是,strcoll()函數(shù)進行的比較是 區(qū)分大小寫的 ,就像strcmp()函數(shù)一樣,在比較過程中對大寫和小寫字母進行了區(qū)別對待。愛掏網(wǎng) - it200.com但是,它不像strcmp()函數(shù)那樣是二進制安全的。愛掏網(wǎng) - it200.com
注意:strcoll()函數(shù)是區(qū)分大小寫的,與strcmp()函數(shù)不同,它不是二進制安全的。愛掏網(wǎng) - it200.com
strcoll(str1,str2);
參數(shù)
此函數(shù)接受兩個字符串作為參數(shù)傳遞,必須要傳入。愛掏網(wǎng) - it200.com以下是對參數(shù)的描述:
- $str1(必須) - 這是函數(shù)的第一個參數(shù),用于比較。愛掏網(wǎng) - it200.com
- $str2(必須) - 這是函數(shù)的第二個參數(shù),用于比較。愛掏網(wǎng) - it200.com
兩個參數(shù)都必須傳遞給函數(shù)。愛掏網(wǎng) - it200.com
strcoll()的返回值
strcoll()返回一個隨機整數(shù)值,其取決于匹配條件。愛掏網(wǎng) - it200.com
返回0 - 如果兩個字符串相等,即 $str1 = $str2 ,則返回0。愛掏網(wǎng) - it200.com
返回 <0 - 如果第一個字符串小于第二個字符串,即 $str1 < $str2,則返回負值(<0)。愛掏網(wǎng) - it200.com
返回 >0 - 如果第一個字符串大于第二個字符串,即 $str1 > $str2,則返回正值(>0)。愛掏網(wǎng) - it200.com
注意:它計算字符串的ASCII值,然后比較兩個字符串以檢查它們是否相等、大于或小于。愛掏網(wǎng) - it200.com
示例1
<?php
str1 = "hello php";str2 = "hello php";
echo strcoll(str1,str2). " because both strings are equal. ";
echo strcoll("Hello PHP", "Hello"). " because the first string is greater than the second string.";
?>
輸出:
0 because both strings are equal.
1 because the first string is greater than the second string.
示例2
<?php
echo strcoll("Hello php", "hello"). " because the first string is less than the second string.";
echo "</br>";
echo strcoll("hello", "Hello"). " because the first string is greater than the second string.";
?>
輸出:
-1 because the first string is less than the second string.
1 because the first string is greater than the second string.
示例3
<?php
echo strcoll("Hello ", "HELLO"). " because the first string is greater than the second string.";
echo "</br>";
echo strcoll("Hello php", "Hello php Hello"). " because the first string is less than the second string.";
?>
輸出:
1 because the first string is greater than the second string.
-1 because the first string is less than the second string.
下面是一個表格,其中包含了一些簡單的示例和strcoll()函數(shù)的解釋,以便更快地理解它。愛掏網(wǎng) - it200.com
字符串1 | 字符串2 | 輸出結果 | 解釋 |
---|---|---|---|
javatpoint | javatpoint | 0 | 兩個字符串相同且相等。愛掏網(wǎng) - it200.com |
javatpoint | JAVATPOINT | 1 | 字符串1 > 字符串2,因為J的ASCII值為74,而j的ASCII值為106,所以j > J。愛掏網(wǎng) - it200.com它區(qū)分大小寫。愛掏網(wǎng) - it200.com |
JAVATPOINT | javatpoint | -1 | 字符串1 < 字符串2,因為J的ASCII值為74,而j的ASCII值為106,所以J < j。愛掏網(wǎng) - it200.com |
javaTpoint | javatpoint | -1 | 字符串1 < 字符串2,因為T的ASCII值為84,而t的ASCII值為116,所以T < t。愛掏網(wǎng) - it200.com |
Java | Java | 1 | 字符串1 > 字符串2 |
Javatpoint | java | -1 | 字符串1 < 字符串2,因為J的ASCII值為74,而j的ASCII值為106,所以J < j。愛掏網(wǎng) - it200.com這里不檢查字符串長度。愛掏網(wǎng) - it200.com |