php中常用的字符串比较函数strcmp()实例解释

时间:2023-03-09 18:16:40
php中常用的字符串比较函数strcmp()实例解释

int strcmp ( string $str1 , string $str2 )

以二进制方式进行比较
以该函数比较时区分大小写
返回值,如果str1小于str2返回<0,如果str1大于str2返回>0如果两者相等返回0。

<?php
$str1 = "hello world"; //$str1与$str3的值相等
$str2 = "HELLO WORLD";
$str3 = "hello world"; //$str1与$str3的值相等 echo strcmp($str1,$str2); //输出1
echo strcmp($str2,$str1); //输出-1
echo strcmp($str1,$str3); //输出0
?>

还有个函数strcasecmp(),用法与strcmp()类似,只是不区分大小写而已,这里不再啰嗦了