<?php //字母 a k $str1 = 'a'; $str2 = 'k'; $res = strcmp($str1,$str2); echo $res; echo "<br />"; //输出-1 $res = strcmp($str2,$str1); echo $res; echo "<br />"; //输出 1 //数字: 1 ,2 ,10, 12 $arr = array(10,1,2,19999999); usort($arr,"strcmp"); print_r($arr); echo "<br />"; //Array ( [0] => 1 [1] => 10 [2] => 19999999 [3] => 2 ) //字符串 $str_arr = array('1','1.1','1.1.1','1.1.1.1','2','2.1','2.1.1','2.2'); usort($str_arr,"strcmp"); print_r($str_arr); echo "<br />"; //Array ( [0] => 1 [1] => 1.1 [2] => 1.1.1 [3] => 1.1.1.1 [4] => 2 [5] => 2.1 [6] => 2.1.1 [7] => 2.2 ) //中文是怎么排序的呢,仔细想想,在新华字典中的排序,是按照拼音的, //比如,a ai an ang ao //ba bai ban bang //那么我们就比较这些值吧,看一下 $str_cn = array('吧','啊','白','奥','昂','爱','安','般','帮'); usort($str_cn,"strcmp"); print_r($str_cn); echo "<br />"; //Array ( [0] => 吧 [1] => 啊 [2] => 奥 [3] => 安 [4] => 帮 [5] => 昂 [6] => 爱 [7] => 白 [8] => 般 )
在java中compateTo()比较的是,字典排序,基于字符串中各个字符的Unicode,php。。可能与之类似。。