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

180it 2020-01-03 PM 1716℃ 0条

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()类似,只是不区分大小写而已,这里不再啰嗦了

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

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