好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php判断字符串是否相等

PHP字符串比较函数主要有strcmp,strcasecmp,strnatcmp,strnatcasecmp,用法基本相似。 (推荐学习:PHP编程从入门到精通)

//按字节对字符串进行比较
int strcmp(string str1,string str2)
//同上,但是不区分大小写
int strcasecmp(string str1,string str2)
//按“自然排序”进行比较
int strnatcmp(string str1,string str2)
//同上,但是不区分大小写
int strnatcasecmp(string str1,string str2)

自然排序和字典排序

字典排序:按照字节的ASCII进行逐字节的比较

自然排序:按照人的思维,比如字节排序中“2”>“11”,而自然排序中“2”<“11”

前面的四个比较函数中,只需要了解这两种排序是什么之后,就可以分清楚四个函数了。举一个最直观的例子:

hello11和hello2的比较,在字典排序中,hello11

<?php
$str1 = 'hello11';
$str2 = 'hello2';
$str3 = 'Hello11';
$str4 = 'Hello2';

echo strcmp($str1,$str2).'<br>';//-1
echo strcasecmp($str1,$str3).'<br>'; //0
echo strnatcmp($str1,$str2).'<br>'; //1
echo strnatcasecmp($str2,$str4).'<br>';  //0

以上就是php判断字符串是否相等的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于php判断字符串是否相等的详细内容...

  阅读:39次